Lecture Notes for Introduction to Computer Science II

24 May 2001 - The String Data Type


  1. character arrays and strings

    1. character arrays - char name[32];

    2. character arrays contain strings

      1. sequence of characters

      2. terminating 0 (or null) byte - important

      3. literals - "hello world!"

    3. good - fairly efficient, fits with the rest of c++

    4. bad - arrays are dangerous; strings are clumsy; the combination of arrays and strings are deadly

      1. arrays are dangerous because the array bounds are not checked and they have fixed size

      2. char name[10];
        name[-100] = 'a';
        name[100] = 'a';
        

      3. the problem need not be severe - name[10] = \0;''

      4. strings are clumsy because of the null byte, subroutine manipulations, and comparisons

      5. arrays and strings are deadly because

        1. strings can overflow arrays with no warning

        2. dynamic strings and static arrays

        3. the null byte is often forgotten

  2. do not use character arrays - they are hard to work with and dangerous

    1. sometimes you have to - command-line arguments; system call arguments

  3. what to use instead - the c++ standard string data type

    1. get the terminology straight - character arrays vs. strings

    2. strings are defined in the header file string

      1. note the absence of the .h - string.h is something completely different

      2. why strings are better than character arrays

        1. strings don't have a null byte - nothing to forget, nothing to remember

        2. strings manage space themselves - no overflows

        3. comparisons work as expected

      3. string declarations and initialization

        1. use #include <string> to get access to strings

        2. part of the standard c++ library - need the std:: prefix

          1. use the using std::string; statement to forego the std:: prefix

        3. strings can be initialized in the usual way

          1. std::string name = "jacaranda";

          2. a string with no initialization is the empty string - the string with no characters

        4. strings are accessed with subscripts, just like arrays

          1. the index range is the same

          2. the length() function returns the number of characters in a string

          3. unfortunately, subscript access to strings is not checked

          4. the at() accessor is checked

      4. string assignment and access

        1. assignment behaves like normal

        2. strings are accessed with subscripts, just like arrays

          1. unfortunately, subscript access to strings is not checked

          2. the at() accessor is checked

        3. + does string catenation

      5. string comparison == works as expected

      6. stream io operations

        1. >> and << work as expected - eats white space

        2. str::string name;
          cin >> name;
          

        3. the getline() function doesn't eat white space

      7. strings and character arrays

        1. automatic (mostly) conversion from character arrays to strings

        2. conversion from strings to character arrays is not automatic, but it is possible

        3. characters are not character arrays - conversions between a character and a string requires work


This page last modified on 29 May 2001.