1.scanf
CHARSTR[15];
scanf ("%s", str);
ABC 123
1) do not read into spaces and enter, end from space
2) The input string is longer than the number of character array elements no error
3) When the input is a character pointer, the pointer must already point to the determined contiguous storage unit with sufficient space
4) When an array element address is set, the address of this element is stored
printf ("%s", address value)
The output encounters the first ' + ' so far
2.gets and puts functions
The opening must be stdio.h;
When the get input includes a space character, it encounters a carriage return end
Puts encounters the first '% ' end and automatically adds line breaks
3.fgets
When using the Fgets function, the fgets function does not write out the array bounds as long as the second parameter is exactly equal to the number of bytes of the array to which the first argument is passed. Therefore, the Fgets function is the best choice.
Fgets (...) Two situations when reading a text line.
1). If n is greater than the string length of a line, fgets (..) when the line break at the end of the string is read. will return. And at the end of s the insertion string ends with the sign ' \ s '. The remaining positions of the S buffer are no longer populated.
Example
123abc
Fgets (S,10,FP);
At this point, read in seven characters, 123abc\n, and actually have the last ' \ ', so, strlen (s) = 7, if you want to remove the end of the \n,s[strlen (s) -1]= ' n ';
2). If n is less than or equal to the length of a string, then a n-1 character is read, and it is not read at this time because it is not at the end of the line, and it will be inserted at the end.
Example
123abc
Char s[5];
Fgets (S,5,FP);
At this point read 4 characters, 123a, and no newline character, so strlen (s) =4.
4.cin
Use Whitespace (spaces, tabs, and line breaks) to set the bounds of a string.
5.getline ()
Reads the entire row of data, using the line break entered by the ENTER key to determine the end of the input.
Calling method: Cin.getline (str, len);
The first parameter, STR, is the array name used to store the input rows, and the second parameter, Len, is the number of characters to read.
6.get ()
Calling method: Cin.get (str, len);
getline will discard the newline character, and get () retains the newline character in the input sequence, but there is no line break in the string .
Use cin.get () when you enter multiple rows of data, the middle can use get () eliminate line breaks.
int main ()
{
Char str1[30], str2[30];
Cin.get (STR1, 30);
Cin.get ();
Cin.get (STR2, 30);
cout << "str1:" << str1 << Endl;
cout << "str2:" << str2 << Endl;
return 0;
}
Comparison of string input methods (with carriage return without carriage return input)