Read strings with spaces in C Language
Http://blog.csdn.net/pipisorry/article/details/37073023question: scanf (% s, a); run hello world
Enter
The input to a is only the part before the space. How can I output the part after the space?
1.
scanf
(
%[^ ]
, str );
#include
int main(){ char str[50]; scanf( %[^], str ); printf( %s, str ); return 0;}
More:
Regular Expression in scanf
1. Customize your own scan set % [abc], % [a-z], % [^ abc], % [^ a-z], compared with isdigit () isalpha () is more flexible. [] Is a matched character, ^ indicates reverse set.
Int I;
Char str [80], str2 [80];
// Scanf (% d % [abc] % s, & I, str, str2 );
// Printf (% d % s, I, str, str2 );
// Scanf (% [a-zA-Z0-9], str );
// Scanf (% [^ abce], str );
Scanf (% [^ a-z], str );
Printf (% s, str );
2. read an address and display the content of the memory address
Int main (void ){
Char ch = 'C ';
Printf (% p, & ch); // print the address of ch.
Char * p;
Cout < Scanf (% p, & p); // input the address displayed abve
Printf (Value at location % p is % c, p, * p );
Return 0;
}
3. Discard unwanted blank spaces: scanf (% c );
4. Non-blank characters in the control string: as a result, scanf () reads and discards a matching character in the input stream. % D, % d;
5. compress the input: Add * Before the format code, and the user will be able to tell scanf () to read this field without assigning it to any variable.
Scanf (% c % * c, & ch); you can use this method to eat excessive carriage returns during character processing.
2. scanf ()
int t[999];int sum=0;while(scanf(%c,&t[sum++])!=EOF);
3. gets ()
# Include
// Char * fgets (char * str, int num, FILE * stream); int main () {char buffer [10]; // fgets (buffer, 10, stdin ); // gets (buffer) with carriage return; // no carriage return printf (% s, buffer); return 0 ;}
More: gets () does not check the string capacity. It is possible to write data out of bounds. You may enter a string of up to 300 bytes or even 2 K Bytes instead of playing the cards according to the regular expression, this is dangerous.
Although strlen, strcyp, and so on do not check whether the pointer is null or writable for high efficiency, these functions are operated by C programmers, C programmers are qualified to use functions correctly. However, gets () is input by the user, and the user does not know the upper limit of the string. Even if you know it, it is not necessarily qualified to use it as required. Pay more attention to it.
Use fgets (str, 80, stdin); it is not dangerous, And fgets is safer.