Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output is: lastName, firstName middleInitial.Ex: If the input is:Pat Silly Doethe output is:Doe, Pat S.If the input has the form firstName lastName, the output is lastName, firstName.Ex: If the input is:Julia Clarkthe output is:Clark, JuliaHere is the code that I have currently:string userName;int space1 = userName.find(' ');int space2 = userName.find(' ', space1 + 1);string firstName = userName.substr(0, space1);string lastName = userName.substr(space2 + 1);char midInitial = userName.substr(space1 + 1);getline(cin, userName);if (userName.find(' ', space1 + 1)) != string::npos) {cout << lastName << ", " << firstName << " " << userName.substr(space1 + 1) << "." << endl;Please help me with this program using c++.

Question
Asked Oct 14, 2019
620 views

Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output is: lastName, firstName middleInitial.

Ex: If the input is:

Pat Silly Doe

the output is:

Doe, Pat S.

If the input has the form firstName lastName, the output is lastName, firstName.

Ex: If the input is:

Julia Clark

the output is:

Clark, Julia

Here is the code that I have currently:

string userName;
int space1 = userName.find(' ');
int space2 = userName.find(' ', space1 + 1);
string firstName = userName.substr(0, space1);
string lastName = userName.substr(space2 + 1);
char midInitial = userName.substr(space1 + 1);

getline(cin, userName);


if (userName.find(' ', space1 + 1)) != string::npos) {
cout << lastName << ", " << firstName << " " << userName.substr(space1 + 1) << "." << endl;

Please help me with this program using c++.

check_circle

Expert Answer

Step 1

This program can be done in a very easy way.

 

Programming instruction:

 

  • First, take the input from the user using “cin”.
  • Then, check how many parts it has.
  • Then, print the name in appropriate format according to the parts of the name.
Step 2

Complete program:

 

#include <iostream>

#include <string>

using namespace std;

 

int main()

{

 

  string first, second, third, total_name;  //Declare variables

  char ch;        

 

  cout << endl;

  cout << "Enter your name (first middle last): "; //Prompt user to enter the name

  cin >> first >> second;  //Taking input in the appropriate variables

 

  if (cin.peek()  != \' \') {   //If the next character in the input sequence is not \' \' then take the third part of the name

    cin >> third;  //Taking input in the "third" variable.

    total_name = third + ", " + first + " " + second.at(0) + ". ";  //store the name in the appropriate format in the variable

    cout << "Your name is: "; 

    cout << total_name << endl;  //Print the name

  }

 

  else {

    total_name = second + ", " + first;  //store the name in the appropriate format in the variable

    cout << "Your name is: ";

    cout << total_name << endl;  //Print the name

  }

 

    return 0;

}

Step 3

Sample Output 1:

Sample Output 2:

 

...
help_outline

Image Transcriptionclose

Enter your name (first middle last): Pat Silly Doe Your name is: Doe, Pat S. Enter your name (first middle last): Julia Clark Your name is: clark, Julia

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Create a conditional expression that evaluates to string "negative" if userVal is less than 0, and "...

A: To create a conditional expression, by adding some code statements to the given program code to chec...

question_answer

Q: I can't figure this one out for the life of me. I've been stuck on it for 4 hours now... HELP! The p...

A: import re inputs = 0numbers_sum = 0 # enter the stringstring = input('Enter a string: ') # condition...

question_answer

Q: Let F = (V; A) be a digraph. Show that if u is k-edge-connected to v and v is k-edge-connected to w,...

A: Connectivity:To measure the connectedness of a digraph ‘F’, consider the minimum number of vertices ...

question_answer

Q: What is the forensic recovery process for files on a solid state drive?

A: Forensic recovery process is different from the other recovery processes, but it might produce the r...

question_answer

Q: Beacon frames in the frequency hopping spread spectrum variant of 802.11 contain the dwell time. Do ...

A: NO,  as we know the Bluetooth frame has dwell time of 625p sec for basic data rate in the frame its ...

question_answer

Q: Please explain, as simplistically (but thoroughly) as possible, how to develop a k-map from a truth ...

A: Rules to be followed to simplify the Boolean expression are given below-

question_answer

Q: The formulas for calculating BMI are B M I = 703 ⋅ w e i g h t I n P o u n d s/ h e i g h t I n I n ...

A: Program code #1:

question_answer

Q: Explain in at least a paragraph what are considered pervasive security infrastructure risks

A: Pervasive security infrastructure refers to cloud security risks. As many users possibly from differ...

question_answer

Q: in python 2. Now write a program named filereader.py that reads and displays the data in friends.txt...

A: The following python program is created that reads and displays data in a file named as friends.txt ...