206k views
4 votes
Create a program that asks for the user's first, middle, and last name as one input. The names must be stored in three different character arrays. The program should then store in a fourth array, the name, arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the first initial of the middle name and a period. For example, if the user entered "Carol Lynn Smith", it should store "Smith, Carol Lynn" in the fourth array. Display the contents of the fourth array on the screen.

User Momocow
by
5.2k points

1 Answer

2 votes

Answer:

Here is the C++ program:

#include <iostream> //to use input output functions

#include <cstring> // used to manipulate c strings

using namespace std; //to identify objects cin cout

const int SIZE = 20; //fixes constant size for firstName, middleName and lastName arrays

const int FULL_SIZE = 60; //constant size for fullName array

int main() { //start of main method

char firstName[SIZE]; //declares a char type array to hold first name

char middleName[SIZE];//declares a char type array to hold middle name

char lastName[SIZE]; //declares a char type array to hold last name

char fullName[FULL_SIZE]; //declares a char type array to hold full name

cout << "Enter first name: "; //prompts user to enter first name

cin>>firstName; //reads first name from user, stores it in firstName char array

cout << "Enter middle name: "; //prompts user to enter middle name

cin>>middleName; //reads middle name from user, stores it in middleName char array

cout << "Enter last name: "; //prompts user to enter last name

cin>>lastName; //reads last name from user, stores it in lastName array

strcpy(fullName, lastName); //copies last name from lastName to fullName array using strcpy method which copies a character string from source to destination

strcat(fullName, ", "); //appends comma "," and empty space " " after last name in fullName using strcat method which appends a string at the end of another string

strcat(fullName, firstName); //appends first name stored in firstName to the last of fullName

strcat(fullName, " "); //appends an empty space to fullName

strcat(fullName, middleName); //appends middle name stored in middleName char array to the fullName char array

cout <<"Full name is:\\"<<fullName<< endl; } //displays the full name

Step-by-step explanation:

I will explain the program with an example inshaAllah

Lets say user enters Carol as first name, Lynn as middle and Smith as last name. So firstName char array contains Carol, middleName char array contains Lynn and lastName char array contains Smith.

Next the fullName array is declared to store Carol Lynn and Smith in a specific format:

strcpy(fullName, lastName);

This copies lastName to fullName which means fullName now contains Smith.

strcat(fullName, ", ");

This appends comma and an empty space to fullName which means fullName now contains Smith with a comma and empty space i.e. Smith,

strcat(fullName, firstName);

This appends firstName to fullName which means Carol is appended to fullName which now contains Smith, Carol

strcat(fullName, " ");

This appends an empty space to fullName which means fullName now contains Smith, Carol with an empty space i.e. Smith, Carol

strcat(fullName, middleName);

This appends middleName to fullName which means Lynn is appended to fullName which now contains Smith, Carol Lynn

Now cout <<"Full name is:\\"<<fullName<< endl; prints the fullName so the output of this entire program is:

Full name is:

Smith, Carol Lynn

The screenshot of the program along with its output is attached.

Create a program that asks for the user's first, middle, and last name as one input-example-1
User Rpggio
by
5.5k points