Source Code. Projects. Nerd Stuff. Art Stuff.

Lesson 6: Cstrings

String Variables

String variables (cstrings)

  • People use a few different methods to represent strings in C++. The most basic is the c-style string, also called cstrings. Another common one is the string class in the standard library (Section 11.2), which we’ll cover later this semester.
  • cstrings are really arrays of characters that are terminated with the null character: �\0� (ASCII character number 0). That way we don�t have to keep track of the array size with a separate variable. The null character is essentially a way of representing nothing (no character).
  • Characters inside a cstring are accessed just like with other arrays. Remember the difference between strings and characters.
  • String initialization: The following lines of code are different ways of doing the exact same thing (setting an array’s value like this can only be done when it is declared):
      char str[4] = "abc";
    char str[] = "abc";
    char str[] = {'a', 'b', 'c', '\0'};

    However, the following line is not the same as above because it doesn’t terminate the string with a null character, or even leave space for it (The resulting array would be allocated for 3 characters.)

    char str[] = {'a', 'b', 'c'};
  • #include <cstring> gives you the following:
    • For string assignment, use strcpy(dest_str, source_str); NOT dest_str = source_str; (Warning: strcpy doesn�t check to make sure there’s space in the destination string)
    • For string comparision, use strcmp(string1, string2); NOT string1==string2; strcmp returns 0 if strings are the same, negative if string1 comes first in ASCII codes, positive if string2 is less.
    • For string concatenation (adding one string to another), use strcat(target_str, suffix_str); ( Warning: Doesn�t check if enough space)
    • To get length of string (not counting null), use strlen(str); (So str[strlen(str)] == ‘\0′ for any string)
  • Stream output and input with strings:
    • cout and cin work fine with strings, as do file input and output streams. just remember that cin >> str; will skip whitespace.
    • Furthermore, like most other kinds of array access, there is no automatic bounds checking. So if str is declared with a size of 4 above and the user types 10 characters with no whitespace before hitting return, all 10 characters will be stored, overwriting 7 characters worth of data in memory that wasn’t allocated for str. So declare your string variables to be long enough that such problems will be unlikely, but it’s hard to avoid this sort of problem completely. Unless I tell you otherwise, I don’t expect your programs to allow input of arbitrarily long strings of data, so we’ll just live with this possibility of error.
    • Password program: Uses a cstring to get and validate a password for a user.
    • To get entire string (including whitespace) entered by user until they hit the return key, or until they enter a maximum number of characters, use e.g. cin.getline(str, 80); (max 79 chars of input, allowing space for null character) For example:
          char class_title[81];
          cout << "Enter name of class";
          cin.getline(class_title, 81);
          // Exact user entry, up to newline or 80th character
          // will be stored in class_title. e.g. "Programming Fundamentals I"
    • Similarly, you can use getline with file input streams.
    • String to numeric conversion (#include <cstdlib>):
      • atoi(“45″) returns 45. (alphabetic to integer)
      • Similarly, atof(str) returns a float, and, atol(str) returns a long integer.
      • How to input any kind of data and then make it numeric is shown at end of 11.1. (p.663) I’ve copied this file here for us to look at.
  • Now that you understand how to work with string variables, you should understand the alternate version of my delete_repeats program, which uses string variables and a Boolean array representing a checklist of letters.