scanf and gets read the string
Learn more about functions such as scanf ()/getchar () and gets ()
scanf the difference between reading a string with the Gets function
See a paragraph today, roughly say get than scanf () quick, a little surprised, searched, scanf () and gets the difference roughly have a few:
1.SCANF () ignores all whitespace at the beginning of the line and ends the input with a space and line break;
After the SCANF statement is executed using GetChar (), the buffer leaves a newline character,
The get reads a string that starts with any character, ends with a newline, but then discards the newline character and replaces it with a ' + ';
2. in the case of large numbers of data, with get read faster than scanf () more than 10 times times (note: From the PUDN, only a paragraph of the opening words)
3.
First: pay attention to whether the different functions accept whitespace, whether to discard the last carriage return problem!
When reading characters:
scanf () ends the input with space, enter, tab, and does not discard the last carriage return (that is, the carriage return remains in the buffer);
GetChar () ends the input with enter and does not discard the last carriage return;
When reading a string:
scanf () end one input with space, enter, tab
Gets () ends the input with enter (the space does not end), accepts the space, discards the last carriage return!
Second: in order to avoid this problem, you must clear the buffer residual data, can be resolved by the following methods:
The 1:c method provides a function to empty the buffer in the language, so long as the buffer is emptied before reading the data, no problem!
This function is Fflush (stdin).
Method 2: Remove the residual data from the buffer itself.
(To tell the truth this statement I did not read, hehe!) Why is the format control like this! I hope the expert pointed out! )
scanf ("%[^\n]", string);
Experiment code: Word solitaire, enter n and n words to determine if the words are the same
#include <iostream>#include<string.h>using namespacestd;intMain () {intN; Charword_pre[ One]; Charword[ One]; BOOLIsNo =false; while(SCANF ("%d", &n)! =EOF) { //fflush (stdin);GetChar (); IsNo=false; for(intI=0; i<n;i++) { if(i = =0) //scanf ("%s", word_pre);gets (Word_pre); Else { //scanf ("%s", word);gets (word); if(Word_pre[strlen (Word_pre)-1] = = word[0]) {strcpy (Word_pre,word); } Else{isNo=true; Break; } } } if(isNo) printf ("no\n"); Elseprintf"yes\n"); } return 0;}
Language |
C |
C + |
Pascal |
to read numbers |
int n; while (scanf ("%d", &n)! = EOF) { ... } |
int n; while (CIN >> N) { ... } |
var N:integer; ... While not seekeof do begin Read (n); ... End; |
to read characters |
int c; while ((c = GetChar ())! = EOF) { ... } |
char c; while (Cin.get (c)) { ... } |
var C:char; ... While not EOF do begin Read (c); ... End; |
to read lines |
char line[1024]; while (gets) { ... } |
string line; while (Getline (CIN, line)) { ... } |
var line:string; ... While not EOF does begin Readln (line); ... End; |
Gets and scanf differences