Monday, November 28, 2011

Lab 8 - string manipulation

You are to complete Lab 8, the programming assignment on page 11 of the course syllabus. You will need to use several of the functions of the string class in order to complete this assignment. I am inserting notes for the functions required in the bullet list of requirements below.

Your C++ program must:

  • Input a name in a single string variable in this order: first_name middle initial last_name.

    You need to read in a whole line with spaces between the data entered. cin by itself won't work since it stops reading at the first occurrence of whitespace. You need to use the getline function. This function accepts all input, including whitespaces, up to the first newline (Enter).

    Required function usage
       string full_name;
       getline(cin, full_name)


  • Find the first name and store it in a separate string variable. Display the first name substring along with its length.

    Once you have the full name, you need to parse out the individual pieces of the name. The family of find functions can help you with this task. find_first_of is the simplest method for you to use to locate the end of the first name. After you know where the first space (' ') is, you use that array index and the substr function to isolate and capture the first name. You display the length of a string by invoking the length method on it.

    Required function usage
       string first_name;
       int pos = full_name.find_first_of(' ');
       string first_name = full_name.substr(0, pos);


  • Find the middle initial and store it in a separate string variable.

    Since the middle initial is a single character, you can seize it based on the position information garnered in the previous bullet point.

    Required function usage
       char m = full_name[pos+1];


  • Find the last name and store it in a separate string variable. Display the last name substring along with its length.

    The last name is pretty easy to acquire once you realize that you can use find_last_of.

    Required function usage
       pos = full_name.find_last_of(' ');
       string last_name = full_name.substr(pos+1);


  • Concatenate a new string with the last name, a comma, a blank, the first name, a blank, and the middle initial. Display the new name and its length.

    This is very simple to do using the append function.

    Required function usage
       string new_name = last_name;
       new_name.append(", ");
       new_name.append(first_name);
       new_name.append(" ");
       new_name.append(1, m);


  • Find and display the position of the comma in the concatenated string.

    Since you know there is one and only one comma in the string you just built, you can locate it with the simple find function.

    Required function usage
       pos = new_name.find(',');


  • Swap the first name and last name strings. Display the names after they are swapped.

    If you pass your strings by reference, you can do this work in a separate function and the effects of the swap will persist in main or you can use the swap function of the string class.

    Required function usage
       first.swap(last)

Sample Output
Here is a screenshot of my code running.

String Manipulation output

Refer to the deliverables post for comment formatting and required program identification information.

Due Date:
Sunday, December 4th, 6:00pm