next up previous
Next: About this document ... Up: SixthWeekBatch1 Previous: Today's Problem

C++ String Class.

  1. We can represent text in C++ using objects of a 'string' class. In order to use this class you should include the following statements:

      #include <string>
      using namespace std;
    

  2. A string can now be created as follows.
      string s = "abc def abc"; 
      string s2 = "abcde uvwxyz";
    

  3. The following table includes methods for reading string input from the stdin and commonly used methods with strings. The variable 'c', used below if of type char and the variable i is of type int.

    Input
    cin >> s;
    The value of s is read from the stdin. The value read stops at whitespace. If "charlie brown" is entered in response to cin>>s, only "charlie" gets stored in s.
    Output
    cout <<s;
    Writes the string to the specified output stream.
    Line input
    getline(cin, s);
    Reads everything up to the next newline character and puts the result into the specified string variable.
    Assignment
    s = s2;
    s = "abc";
    A string literal or a string variable can be assigned to a string variable.
    Subscript
    s[1] = 'c';
    c = s[1];
    Changes s to equal "acc def abc" Sets c to 'b'. The subscript operator returns a char value, not a string value.

    Length

    i = s.length();
    i is set to the current length of the string s
    Empty
    if(s.empty())
    i++;
    The example adds 1 to i if string s is empty
    Relational operators
    if (s < s2)
    i++;
    Uses ASCII code to determine which string is smaller. Here the condition is true because a space comes before letter d
    Concatenation
    s2 = s2 + "x";
    s2 += "x";
    Both examples add x to the end of s2
    Substring
    s = s2.substr(1,4);
    s = s2.substr(1,50);
    The first example starts in position 1 of s2 and takes 4 characters, setting s to "bcde". In the second example, s is set to "bcde uvwxyz". If the length specified is longer than the remaining number of characters, the rest of the string is used. The first position in a string is position 0. The substr method doesn't change the string object on which it is called. i.e. the s2 here is not changed after the substr operation.
    Substring replace
    s.replace(4,3,"x");
    Replaces the three characters of s beginning in position 4 with the character x. Variable s is set to "abc x abc". The replace method does change the string object on which it is called.
    Substring removal
    s.erase(4,5);
    s.erase(4);
    Removes the five characters starting in position 4 of s. The new value of s is "abc bc". Remove from position 4 to end of string. The new value of s is "abc ". The erase method does change the string object on which it is called.
    Pattern matching
    i = s.find("ab",4);
    The first example returns the position of the substring "ab" starting the search in position 4. Sets i to 8. Exercise: What is returned if the substring doesn't exist?

  4. Sample Example:

    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        string s1="calvin";
        cout<<"Length:"<<s1.length()<<endl;
        cout<<s1.empty()<<endl;
        string s2="hobbes";
        string s3=s1+s2;
        cout<<s3.substr(1,4)<<endl;
        cout<<s3<<endl;
        cout<<s3.erase(3,2)<<endl;
        cout<<s3.replace(4,3,"B")<<endl;
        int i=s1.find("lv",2);
        cout<<"i="<<i<<endl;
    }
    

  5. Output:
    Length:6
    0
    alvi
    calvinhobbes
    calnhobbes
    calnBbes
    i=2
    


next up previous
Next: About this document ... Up: SixthWeekBatch1 Previous: Today's Problem
cs101 senior TA 2005-09-12