C - How to read a string with only spaces using scanf -


this question has answer here:

i'm little problem while doing excercise learn c. problem is: need read string user, if types space, need print space. that's okay in theory. but, when type space while program running, doesn't understand string , keeps waiting me type other things.

i'm using scanf("%[^\n]", string_name_here);

i appreciate help, , have nice day! o/ , sorry bad english, hope can understand :)

using char *fgets(char *str, int n, file *stream) make day.

according man :

fgets() reads in @ 1 less size characters stream , stores them buffer pointed s. reading stops after eof or newline. if newline read, stored buffer. terminating null byte ('\0') stored after last character in buffer.

example program

#include <stdio.h> #define maxstr 21  int main(void) { char my_str[maxstr] = {'\0'};  fgets(my_str, maxstr, stdin);  return 0; } 

input :

claudio cortese

output :

claudio cortese


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -