-
The character '\0' is called null character and is used as an end marker because it is distinct from all the "real" characters.
-
A string stored in this (as as array of characters terminated with '\0') is called a C-String.
-
A C-String variable is just an array of characters.
-
A c-string variable is a partially filled array of characters.
-
A c-string variable does not use an int variable to keep track of how much of the array is currently being used. Instead, a string variable places the special symbol '\0' in the array immediately after the last character of the c-string.
-
A c-string variable is declared as:
char arrayName[max_c_string_size + 1];
-
When you intialized a c-string variable, you can omit the array size. C++ will automatically make the size of the c-string variable 1 more that the length of the quoted string.
char shortString[] = "abc";
is the same as
char shortString[4] = "abc";
-
A c-string variable is an array, it has indexed variables that can be used just like those of any other array.
-
The ordering relationship used for comparing characters is called lexicographic order.
-
The member function getline can be used to read a line of input and place the c-string of characters on that line into a c-string variable, as in:
cin.getline(stringVariable, maxCharacters + 1);
-
A class string allows the programmer to treat strings as a basic data type without needing to worry about implemenation details.
-
The class string allows you to treat string values and string expressions very much like values of a simple type. You can use the = operator to assign a value to a string variable and you can use the + sign to concatenate 2 strings.
-
The extractor operator >> can cin work the same for string objects as for other data, but remember that the extractor operator ignores initial whitespace and stops reading when it encounters more whitespace.
-
If you want your program to read an entire line of input into a variable of type string, you can use the function getline. You do not use cin.getline, instead cin is the first argument, as in:
getline(cin, line;
-
You cannot use cin and >> to read in a blank character. If you want to read 1 character at a time, you can use cin.get. The fucnction cin.get reads values of type char, not of type string, but it can be helpful when handling string input.
-
Every string object has a member function named length that takes no arguments and returns the length of the string represented by the string object.
-
A c-string vaiable is the same thing as an array of characters but it is used in a slightly different way. A string variable uses the null character '\0' to mark the end of the string stored in the array.
-
C-string variables usually must be treated like arrays, rather than simple variables of the kind we used for numbers and single characters.
-
A c-string value cannot be assigned to a c-string variable using the = sign and you cannot compare the values of 2 c-string variables using the = operator. Special functions are used to perform these tasks.
-
Objects of the class string are better behaved that c-strings. The assignment and equal operators, = and ==, have their intuitive meaning when used with objects of the class string.
|
|