Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used for loop to scan strings... n is used for the number of strings I scan.
After scanning all the strings I tried to print those strings which have not more than ten characters. I used t[i] to print strings. But it's not working..
[sorry for my bad english]

What I have tried:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define size_limit 100
int main(){
int n, i, j, alp;
scanf("%d\n", &n);
int h[n], numint;
char k[n], z[n], t[n];
char str[size_limit], first, last, print[100];
for(i=0; i<n; i++){
        alp=j=last=0;
    fgets(str, sizeof str, stdin);

    while(str[j]!='\0') {
        if((str[j]>='a')&&(str[j]<='z')){
            alp++;
first=str[0];
 last=str[alp-1];
 numint=alp;

        }
        j++;
    }
k[i]=first;
z[i]=last;
h[i]=numint;
t[i]=str;


}
for(i=0; i<n; i++){
if(h[i]<=10){
printf("%s", t[i]);
}else{
printf("%c%d%c\n", k[i], (h[i]-2), z[i]);
}
}
return 0;
}
Posted
Updated 6-Nov-21 5:48am

First off, do yourself a favour and learn to indent your code properly: even with a tiny scrap of code like you show, it becomes much, much more readable if you indent it. That's all over the place and it's not simple to work out what is supposed to be happening.

Then, stop using single character names for everything: use descriptive variable name which tell the reader what they are used for: h, k, z, t: I have no idea what you expect them to contain! You may understand them today, but in three weeks time when you come back to it you will have as little idea as I do ...
It may seem like it just adds typing for you, but it makes your code more readable, and more reliable: it's a lot harder to mistype a name and get a valid variable if you use "proper names".

The problem is that you don't save any strings: each time round your loop, you read a string from the user into the same buffer, and then try to build up some kind of result in a tiny buffer: if I say I'll provide three strings, and then type 99 characters in each, where does your code store those 279 characters for printing?

What I would do is read each string, check it's length, then print it if it is shorter than eleven characters: the code becomes a simple loop to read and process a string, with a very quick check inside that to check the length and print it or not.

Think about it: if you were reading a book and collecting each short sentence, you would read them one by one, check the length, and copy the short ones to a new file or piece of paper. So duplicate what you would do for this task.

You need to start spending time thinking about the task and working out a "complete approach" to solving it before you dive into code: at the moment your all of the code in your questions looks like it has been dived right into code at the start, and nothing thrown away even if it turns out to be a silly direction. Five minutes planning can save you hours of wasted effort!
 
Share this answer
 
Comments
Jahirul Sarker 8-Nov-21 2:00am    
"The problem is that you don't save any strings"--- I am looking for a way to save all the strings but I can't find any. Please give me some hints.
OriginalGriff 8-Nov-21 2:33am    
If you want to save strings, you need to allocate space for them, and free that space when you are finished with it. And while that possible - even easy - using malloc and free, that doesn't mean it;'something I would expect a beginner to jump into as it is fraught with problems if you don't know what you are doing. Which doesn't seem to be the case yet! :laugh:

That's why I wouldn't do it by saving: I'd process each string as I read it and skip the "saving" step altogether.
In addition to what the others have said, you should turn up the warning on your compiler. Since you use the idiom
C
int n;
scanf("%d", &n);
char str[n];
I assume you are using linux or mingw, since MSVC does not accept "variable length arrays", in which case you should add (at least)
-Wall -Wextra
to the compile command. If you had done that, you would have got a warning about this:
C
char t[n];
char str[100];
/ * ... assignment of str ommited  ... */
for(i = 0; i < n; ++i)
{ 
    /* ... code omitted ... */
    t[i] = str;  /*  Here we are attempting to copy str in to t[i] */
                 /*  but that's not how you do that */
     /* ... code omitted ... */
}
Some other observations:
scanf("%d\n", &n) doesn't do what you think. Try this and see what happens:
C
int n;
char str[100];
printf("How may strings? ");
scanf("%d\n", &n);
int i;
for(i = 0; i < n; ++i) {
    printf("Enter string %d : ", i);
    fgets(str, sizeof str, stdin);
}


What happens if the user enters more than 100 chars?
What happens if the user enters the string "ABCDEFGHIJKLMNOP"?
What happens if the user enters the string "abcd efgh"?
 
Share this answer
 
Comments
Jahirul Sarker 7-Nov-21 23:14pm    
"warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]|"
I use codeblocks 20.03 in windows 10
Jahirul Sarker 7-Nov-21 23:22pm    
"Some other observations:
scanf("%d\n", &n) doesn't do what you think. Try this and see what happens:
int n;
char str[100];
printf("How may strings? ");
scanf("%d\n", &n);
int i;
for(i = 0; i < n; ++i) {
printf("Enter string %d : ", i);
fgets(str, sizeof str, stdin);
}"
yes you are right.
Even I see some skilled coding in your post you must learn the language better and know about the standard C library which provides a bunch of functions like string handling.
Invest some time to learn about it, because it will help you everytime you write code.

A seasoned craftsman knows his tools and so can use the optimal tool for every job. ;-)

PS: printf can also print strings
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900