Hi, I just want to know how I can get a string from stdin which contains whitespaces? I tried fgets and scanf("%[^\n]",str) but it's still not working
asked 22 Feb '14, 19:34
If you want to input many strings in one line with white spaces then you can try the following code:-
But if the input consists of many integers (the number of integers being unspecified) then try the following code :-
I always use
Works every time!!
answered 25 Feb '14, 06:36
Here is what i do : Use gets to input the string. If there was a scanf statement before the gets the use getchar to consume the extra '\n' character that remains in the input buffer.
Its a crude approach but works in most questions.
answered 22 Feb '14, 19:45
answered 22 Feb '14, 20:19
use getline() function
answered 22 Feb '14, 22:34
In GCC you can use
scanf("%10[0-9a-zA-Z ]s", str);
scanf("%10[0-9a-zA-Z ]", str);
It probably wont work with other compiler, but most people in coding contests use either gets(str) or getline(cin, str)
answered 23 Feb '14, 00:34
let an array char b;
you should do fflush(stdin); fgets(b,51,stdin);
answered 24 Feb '14, 14:54
Use the following:
string s; getline(cin,s);
answered 25 Feb '14, 09:34
TO SCAN STRING ENDING WITH NEWLINE CHARACTER : SCAN STRING WITH SPACE WORK EVERY TIME I TRUST ON IT
scanf ("%[^\n]%*c", str);
answered 07 Jun '15, 08:45
This answer is marked "community wiki".
answered 11 Jan, 17:34