Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, use the standardized string object data type from the ANSI/

ID: 3773556 • Letter: F

Question

For this assignment, use the standardized string object data type from the ANSI/ISO Standard C++ library. This library is #include <string>

Write a C++ program that does the following:

Input a name in a single string variable in this order: first name middle initial last name.

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

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

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

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.

Display the length of the concatenated string.

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

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

Your output should resemble the following:

Enter a first name, middle initial, last name: Katherine G Mills

The 9 characters of the first name are: Katherine

The 5 characters of the last name are: Mills

In the phone book, the name would be: Mills, Katherine G

The length of the name is: 18

The comma is at position: 5

After the swap, the last name is Katherine and the first name is Mills

Here's what I have please fix I cant get it right:

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
int main(int argc, char** argv) {

string fullName = "";
string firstName = "";
string middleInitial = "";
string lastName = "";

int i;
int level = 0;
for(int i = 0; i < fullName.length(); i++){
char ch1 = fullName[i];
if(ch1 == ' '){


cout << "Enter a first name, middle initial, last name: ";
cin >> fullName;
{
level++;
}
}
else
{
if(level == 0)
firstName = firstName + (string)ch1;

if(level == 1)
middleInitial = middleInitial + (string)ch1;

if(level == 2)
lastName = lastName + (string)ch1;
}
cout << "The " << firstName.length() << " characters of the first name are: ";
cout << firstName << endl;

cout << "The " << lastName.length() << " characters of the last name are: ";
cout << lastName << endl;

cout << "In the phone book, the name would be: " << lastName << ", " << firstName;
cout << middleInitial << endl;

cout << "The length of the name is " << fullName.length() << endl;

cout << "The comma is at position: " << lastName.length() << endl;

cout << "After the swap, the last name is " << firstName << " and the first name is ";
cout << lastName << endl;
}

Explanation / Answer

Sample code:

Sample output:

Code to copy:

// lastname.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <string>

#include <iostream>

#include <iomanip>

#include <fstream>

using namespace std;

int main() {

string fullName = "";

string firstName = "";

string middleInitial = "";

string lastName = "";

string fullName1 = "";

cout << "Enter a first name, middle initial, last name: ";

std::getline(std::cin, fullName);

int index = fullName.find(" ");//finds index of the space

string temp = fullName.substr(0, index);

cout << "The " << temp.length() << " characters of the first name are: ";

cout << temp << endl;

lastName = fullName.substr(index + 1);//get the substring

int index1 = lastName.find(" ");

string tempmiddle = lastName.substr(0, index1);

string temp1 = lastName.substr(index1 + 1);

cout << "The " << temp1.length() << " characters of the last name are: ";

cout << temp1 << endl;

fullName1 = temp1 + ", " + temp + " " + tempmiddle;//name format in phone book

cout << "In the phone book, the name would be: " << fullName1 << endl;

cout << "The length of the name is: " << fullName1.length() << endl;

cout << "The comma is at position: " << fullName1.find(",") << endl;

cout << "After the swap, the last name is " << temp << " and the first name is ";

cout << temp1 << endl;

system("pause");

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote