Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to scan a blank string before a char string....but im unable to do that

input
2

abc

expected output

abc

actual output
abc
abc

below i have given my code.....

i have tried fgets too but it shows the same result as in gets case

What I have tried:

#include <stdio.h>
#include <string.h>
int main() 
{
  char s[10000]; 
    int t;
    scanf("%d\n",&t);
   while(t>0)
     {   
         
         gets(s);
         puts(s);
       
      t--;  
     }
	
	return 0;
}
Posted
Updated 16-Aug-18 10:25am
v4
Comments
Patrice T 28-Jun-18 17:37pm    
May be you should explain what is supposed to do your program !
An example input and out put wouldn't harm either.
OriginalGriff 29-Jun-18 0:33am    
Start by indenting your code properly - I've done that for you - so that it's actually readable.
Then sort out your variable names: one character variable may be easy to type, but they do not make c0ode readable as I have no idea what any of them are supposed to do. Calling "s" "source" or similar make take an extra character in modern IDEs but it makes the code self documenting and a lot clearer.

At the moment, I have no idea what your code is supposed to be doing, or what it actually does!
Richard MacCutchan 29-Jun-18 7:31am    
Looks to me like your code is working. Don't forget that the line you type in will show on the console before it is echoed by the program. Hence abc twice.
Richard MacCutchan 29-Jun-18 8:30am    
I have tested this and it appears that gets does not return anything if no input is typed.
Member 13887543 29-Jun-18 8:39am    
ieven if i am giving ' '(space) as a character it is not reading it.
space must be printed as they r characters .....thats not a blank string

1 solution

Just add the following line right after the puts( s ) line:


s[0] = 0;
 
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