help with String Library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carly
    New Member
    • Oct 2006
    • 37

    help with String Library

    Hi all,

    I have been working on a quesstion, i have solved parts a up to c, but im having trouble with part d and e, any tips on how i can solve them would be very much appreaciated. I'd also appreaciate if you guys can also tell me if i have correctly solved a,b and c.

    this is the question:

    If string s1 and string s2 are two string variables, containing some strings how do you:
    a.print out the number of characters of s1 to the screen

    answer:

    Code:
     string s1= “Easy”
               cout<<s1.length()<<endl;
    b.concatenate s1 and s2 and store the result in another string s

    answer:

    Code:
    string s = s1+s2;
    c.determine whether s2 contains the string "cool"

    answer:
    Code:
    string s2("CSCI204 is cool");
                              int where = s2.find("cool");
                              cout << "First occurence of cool was found at: " << 
                              where << endl;

    d.display the string s1 in reverse order on the screen



    e.display the lower-case version of the string s2 to the screen (Hint: you may want to create a separate function to return the lower-case version of a single character and use that function to solve this problem).

    Thanks in advanance,

    carly
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by carly
    c.determine whether s2 contains the string "cool"

    answer:
    Code:
    string s2("CSCI204 is cool");
    int where = s2.find("cool");
    cout << "First occurence of cool was found at: " << 
            where << endl;
    d.display the string s1 in reverse order on the screen

    e.display the lower-case version of the string s2 to the screen (Hint: you may want to create a separate function to return the lower-case version of a single character and use that function to solve this problem).
    First, for part c, what if the string did not have "cool" in it? What would .find() return? Depending on your compiler, it might return -1, or some garbage value - in my compiler (Bloodshed DevC++ 4.9.9.2), it returns a value string::npos. You should check to see if "cool" exists in the string before displaying where it is shown, or your code might display

    First occurence of cool was found at: -1
    // OR
    First occurence of cool was found at: 47638238

    which makes no sense whatsoever.

    For parts d and e, consder the following fact:

    A string is simply a collection of characters. (If you have discussed/covered arrays, you know that this collection is an array - a string is an array of characters.) You also know that the length of a string depends on the amount of characters - a space counts as much as a letter, etc. One of the cool things about a string is that you can get any single character if you know its position. A string is positioned in the following manner:

    Code:
             // 0123456
    string s = "Sample."
    // The position of 'm' would be 2
    If you wanted to print the 3rd character in s (which we know is 'm'), you can define a character variable ch and set ch to s[2], or the character at position 2 of s. In this way, you can access each individual character of any string.

    Suppose you wanted to start processing the characters from the back. If the first position is 0, what is the position of the last character? It isn't s.length(), because in our previous example, s.length() is 7, but the characters go from 0 to 6. The last character is at position s.length() - 1.

    Knowing this, you should be able to solve part d. (Note: If you have covered arrays, much of this should make sense. The positioning of each character of a string is the position of that character in an array. s.length() returns how many elements are in the array, and the last element of an array is its size - 1.)

    For part e, all you should need to know is that the header file <cctype> includes the following function:

    Code:
    int tolower(int x);
    // tolower takes the integer representation of a character
    // (for example, 'A' is 65 and 'a' is 97) and returns the integer
    // representing the lowercase version IF the variable x
    // is an uppercase letter - otherwise, tolower returns
    // the same number.
    // Note that this function can be called by passing a character
    // instead of an integer, and will work perfectly well.
    // Also note that the return type is int - to convert to a character, use
    // char ch = static_cast<char>(/*Whatever*/);
    Good luck!

    Comment

    Working...