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

2020-06-21 04:26发布

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?

标签: c string spaces
5条回答
Fickle 薄情
2楼-- · 2020-06-21 04:37

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);
查看更多
一夜七次
3楼-- · 2020-06-21 04:48

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

查看更多
Animai°情兽
4楼-- · 2020-06-21 04:49

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
查看更多
虎瘦雄心在
5楼-- · 2020-06-21 04:58
char str[100];

Try this

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

or this

fgets(str, sizeof str, stdin))
查看更多
放我归山
6楼-- · 2020-06-21 05:01

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.

查看更多
登录 后发表回答