Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a string with spaces in it in C?

Tags:

c

string

spaces

scanf("%s",str) won't do it. It will stop reading at the first space. gets(str) doesn't work either when the string is large. Any ideas?

like image 388
Johny Avatar asked Oct 26 '10 16:10

Johny


2 Answers

use fgets with STDIN as the file stream. Then you can specify the amount of data you want to read and where to put it.

like image 120
NG. Avatar answered Sep 22 '22 04:09

NG.


char str[100];

Try this

 scanf("%[^\n]s",str);

or this

fgets(str, sizeof str, stdin))
like image 20
sujeesh Avatar answered Sep 21 '22 04:09

sujeesh