
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 usagestring 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 usagestring 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 usagechar 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 usagepos = 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 usagestring 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 usagepos = 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 usagefirst.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