0

I need to write a simple program using C language. So I need to input a sentence through the keyboard and display it. Is there any method to allow spaces to be entered using scanf()?

Aye
  • 55
  • 1
  • 9
  • 1
    `scanf` does not help with display. Try `printf`. – Ben Voigt May 06 '19 at 03:05
  • Use `fgets` or `getline` to get a whole line. – Retired Ninja May 06 '19 at 03:07
  • https://stackoverflow.com/questions/3501338/c-read-file-line-by-line – Retired Ninja May 06 '19 at 03:09
  • What constitutes "a sentence"? If it a series of characters up to the first full stop (period), exclamation mark or question mark, you can read it with `scanf()` using scan-sets (carefully). When it comes to printing, there really isn't a problem; the print functions will print whatever string you give them. If you want to replace newlines with blanks except at the end of the sentence, you have to work harder. Leading spaces can be dealt with by judicious use of white space in the `scanf()` format string. – Jonathan Leffler May 06 '19 at 06:22
  • 1
    Possible duplicate of [How do you allow spaces to be entered using scanf?](https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – Kamiccolo May 06 '19 at 13:51

1 Answers1

0

if your question (from the title) is to scan a line with whitespaces,

char string[20];
scanf("%[^\n]s",string);

you can print this by:

printf("%s",string);
bobthebuilder
  • 184
  • 14
  • 3
    `scanf("%19[^\n]",string);` -- there is no `'s'` after `"%[...]"`, the *character-class* is a complete *conversion-specifier* in and of itself. Always use the *field-width* modifier to protect your array bounds. (and I would suggest `char string[256];` or more -- don't skimp on buffer size `:)` Fix those and drop a comment in reply. You are thinking in the right direction, you just need to tidy up the details. – David C. Rankin May 06 '19 at 07:34