SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Strings And Vectors
Exercise:
Programming Projects
Question:4 | ISBN:9780321531346 | Edition: 7

Question

Write a program that reads a person’s name in the following format: first

name, then middle name or initial, and then last name. The program then

outputs the name in the following format:

Last_Name, First_Name Middle_Initial.

For example, the input

Mary Average User

should produce the output:

User, Mary A.

The input

Mary A. User

should also produce the output:

User, Mary A.

Your program should work the same and place a period after the middle

initial even if the input did not contain a period. Your program should

allow for users who give no middle name or middle initial. In that case,

the output, of course, contains no middle name or initial. For example,

the input

Mary User

should produce the output

User, Mary

If you are using C strings, assume that each name is at most 20 characters

long. Alternatively, use the class string. Hint: You may want to use three

string variables rather than one large string variable for the input. You

may find it easier to not use getline.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header file section
#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string firstName, middleName, lastName;
    char middleInitial;

    // Ask the user to input the name
    cout << "Enter your name in the format: First Name Middle Name/Initial Last Name: ";
    cin >> firstName;

    // Check if the user provided a middle name or initial
    if (cin.peek() != '\n') {
        // Read the middle name/initial
        cin >> middleName;

        // If the input contains a period after the middle name/initial, ignore it
        if (middleName.back() == '.') {
            middleName.pop_back();
        }

        // If the middle name/initial is more than one character, consider it as a name
        if (middleName.length() > 1) {
            lastName = middleName;
            cin >> middleInitial;
        } else {
            cin >> lastName;
        }
    } else {
        // If no middle name or initial is provided, directly read the last name
        cin >> lastName;
    }

    // Output the name in the desired format
    cout << lastName << ", " << firstName << " ";
    if (!middleName.empty() && middleName.length() == 1) {
        cout << middleName << ".";
    }
    cout << endl;

    return 0;
}

OUTPUT:

Enter your name in the format: First Name Middle Name/Initial Last Name: John Martin Peter
Martin, John

 

 

0 0

Discussions

Post the discussion to improve the above solution.