C++ Chapter 8: Strings and C-Strings

Card Set Information

C++ Chapter 8: Strings and C-Strings
2012-03-26 15:41:44
strings cstrings

C++ Chapter 8: Strings and C-Strings
Show Answers:

  1. The character '\0' is called null character and is used as an end marker because it is distinct from all the "real" characters.
  2. A string stored in this (as as array of characters terminated with '\0') is called a C-String.
  3. A C-String variable is just an array of characters.
  4. A c-string variable is a partially filled array of characters.
  5. 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.
  6. A c-string variable is declared as:
    char arrayName[max_c_string_size + 1];
  7. 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";
  8. A c-string variable is an array, it has indexed variables that can be used just like those of any other array.
  9. The ordering relationship used for comparing characters is called lexicographic order.
  10. 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);
  11. A class string allows the programmer to treat strings as a basic data type without needing to worry about implemenation details.
  12. 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.
  13. 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.
  14. 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;
  15. 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.
  16. 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.
  17. 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.
  18. C-string variables usually must be treated like arrays, rather than simple variables of the kind we used for numbers and single characters.
  19. 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.
  20. 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.