Input String with spaces

How can I input a string in C with spaces. I know gets and scanf("%[^\n]s") are possible answers but they are not accepted by command line text input files with redirection operators.
Plz answer?

scanf ("[^\n]*c", P);
scanf ("[^\n]*c", C);

try this

1 Like

What are P and C here?

is P a char array or a string variable??

i think u asked this question for solving problem CCODE in December challenge and in that problem i assume char P[27];char C[150010]; and sorry for not mention in previous comment

1 Like

Use this:-

fgets (var, 100, stdin); //100 is the size of this buffer
scanf ("%[^\n]%*c", name);

The [] is the scanset character. [^\n] tells that while the input is not a newline (ā€™\nā€™) take input. Then with the %*c it reads the newline character from the input buffer (which is not read), and the * indicates that this read in input is discarded (assignment suppression), as you do not need it, and this newline in the buffer does not create any problem for next inputs that you might take.

Hopefully you understand! :slight_smile: