How do I get scanf to accept strings with spaces in them, for instance 'Hello World,' whereby it'll take the whole sentence and not just the first word.
Thanks
Printable View
How do I get scanf to accept strings with spaces in them, for instance 'Hello World,' whereby it'll take the whole sentence and not just the first word.
Thanks
scanf("%s %s", variables);
but then you need to know how many words there are going to be.
use
gets, or fgets
Code:char string[256];
fgets(string, sizeof(string), STDIN);
Is gets a reliable function to use? I heard its not. Is it reliable to use in coursework?
Never ever use gets(). Use fgets() instead.
>for instance 'Hello World,' whereby it'll take the whole sentence and not just the first word.
How do you end a sentence? What you want to do moves into the realm of advanced scanf usage, where you'll be relying heavily on scansets. To read an entire line with scanf, assuming a buffer of size 1024, you could do this:
To read a whole sentence, you stop at the first sentence terminating punctuation character:Code:scanf ( "%1023[^\n]", buffer );
Note that in both cases, the character that scanf stops at is left in the stream. Of course, for some reason a lot of people have trouble getting scanf right, so you'd be better off using fgets and then parsing as necessary.Code:scanf ( "%1023[^.?!]", buffer );
>fgets(string, sizeof(string), STDIN);
stdin isn't capitalized in standard C.