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

元气小坏坏 提交于 2019-12-13 11:50:36

问题


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?


回答1:


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.




回答2:


char str[100];

Try this

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

or this

fgets(str, sizeof str, stdin))



回答3:


Create your own function to read a line. Here's what you basically have to do:

1. fgets into allocated (growable) memory
2. if it was a full line you're done
3. grow the array
4. fgets more characters into the newly allocated memory
5. goto 2.

The implementation may be a bit tricky :-)

You need to think about what you need to pass to your function (at the very least the address of the array and its size); and what the function returns when everything "works" or when there is an error. You need to decide what is an error (is a string 10Gbytes long with no '\n' an error?). You need to decide on how to grow the array.


Edit

Actually it may be better to fgetc rather than fgets

get a character
it it EOF? DONE
add to array (update length), possible growing it (update size)
is it '\n'? DONE
repeat



回答4:


When do you want to stop reading? At EOF, at a specific character, or what?

You can read a specific number of characters with %c

c Matches a sequence of width count characters (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating NUL is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

You can read specific characters (or up to excluded ones) with %[

[ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] charac- ter. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a cir- cumflex, in) set or when the field width runs out




回答5:


To read string with space you can do as follows:

char name[30],ch;

i=1;
while((ch=getchar())!='\n')
{
name[i]=ch;
i++;
}
i++;
name[i]='\n';
printf("String is %s",name);


来源:https://stackoverflow.com/questions/4025673/how-can-i-read-a-string-with-spaces-in-it-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!