I did not have a deep understanding of the character string. Now I will discuss some questions about the character string input.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162H9CS-0.png "title =" string.png "/>
1. strlen () returns the length of the string in the array, rather than the length of the array.
2. strlen () only counts visible characters, not empty characters.
So more interesting is the following:
Char name [16] = "abcdefg"; // What is the output result? Cout <name <endl; name [3] = '\ 0'; // What is the output result? Cout <name <endl;
Guess?
# include <iostream># include <cstring># define SIZE 15using namespace std;int main(void){ char name_cin[SIZE]; char name[SIZE] = "C++owboy"; //initialized array cout << "Hello I'm " << name; cout << "! What is your name ? "; cin >> name_cin; cout << "Well " << name_cin << ", your name has "; cout << strlen(name_cin) << " letters and is stored " << endl; cout << "in an array of " << sizeof(name_cin) << "bytes." << endl; cout << "your initial is " << name_cin[0] << "." << endl; name[3] = '\0'; cout << "Here are the first 3 characters of my name : "; cout << name << endl; return 0;}
Let's guess the result?
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162H91316-1.png "title =" string2.png "/>
The name string is truncated...
Meaning:
The array can be indexed to access each character of the array. For example, name [0] finds the first character of the array, name [3] = '\ 0'; is set to null, so that the string ends after the third character, even if there are other characters in the array.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162H94148-2.png "title =" string3.png "/>
However, cin has a defect, that is, it ends with a blank character. If there is a space or press enter, the string is input, so you need to use the method that can enter a line of string to solve the problem, but let's look at this question first:
# Include <iostream> using namespace std; int main (void) {const int ArSize = 20; char name [ArSize]; char dessert [ArSize]; cout <"Enter your name:" <endl; cin> name; // Enter the name cout <"Enter your favorite dessert:" <endl; cin> dessert; // enter the dessert name cout <"I have some delicious" <dessert; cout <"for you," <name <". "<endl; return 0 ;}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162Ha346-3.png "title =" string4.png "/>
Meaning:
Cin uses spaces (spaces, tabs, line breaks) to define the boundary of the string. cin reads only the first word when obtaining the character array input. After reading the word, cin puts this string into the array and automatically adds an empty character '\ 0' at the end'
Cin uses Meng as the first string and puts it in the array. When Liang is placed in the input queue for the second input, it is found that the input queue Liang, because cin reads Liang, and put it in the dessert array.
If a row of data can be entered, will this problem be solved?
Getline () and get () can be implemented...
# include <iostream>using namespace std;int main(void){ const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter you name : " << endl; cin.getline(name,ArSize); cout << "Enter you favorite dessert : " << endl; cin.getline(dessert,ArSize); cout << "I have some delicious " << dessert; cout << " for you," << name << endl; return 0;}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162H94M1-4.png "title =" string5.png "/>
Meaning:
Cin uses a word as the input, and sometimes we need to use a line as the input, such as I love C ++
Iostream provides some row-oriented class member functions, such as getline () and get (). Both functions read the input of a row until the end of the line break. The difference is getline () will discard the line break
Get () retains the linefeed in the input sequence
Row-oriented input: getline (char * cha, int num)
Getline () is used to read the entire row. The end is determined by a line break. You can use cin to call it. getline (char * cha, int num) is used as a member function. The first parameter is used to store the name of the array of the input row, and the second parameter is the number of characters to read, if the number of characters is 30, a maximum of 29 characters are read, and the rest are used to store null characters automatically added at the end.
When get () is used to store strings, it ends with a null character.
What should I do if this happens?
# Include <iostream> using namespace std; int main (void) {cout <"What year was your house built? "<Endl; int year; cin> year; // char ch; // cin. get (ch); receives line breaks (cin> year ). get (); cout <"What is its street address? "<Endl; char address [80]; cin. getline (address, 80); cout <"Year built:" <year <endl; cout <"Address:" <address <endl; cout <"Done! "<Endl; return 0 ;}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/162H91c9-5.png "title =" string6.png "/>
The address has not been entered...
Remove the preceding notes and add a character to receive line breaks...
Note: C ++ programs often use pointers instead of arrays to process strings.
This article from the "_ Liang_Happy_Life _ Dream" blog, please be sure to keep this source http://liam2199.blog.51cto.com/2879872/1202957