15.1k views
4 votes
Write 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. Display the contents of the fourth array on the screen. No standard strings can be used. Oh I'm so mean.

Sample input:
Neil Patrick Harris
Output:
Harris, Neil P.

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, middle, and last name: "; //prompts user to enter first name

cin>>firstName>>middleName>>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

char temp[2]; //creates a temporary array to hold first initial of middle name

temp[0] = middleName[0]; //holds the first initial of middle name

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

strcat(fullName, "."); //appends period

cout<<fullName; } //displays full name

Step-by-step explanation:

I will explain the program with an example

Lets say user enters Neil as first name, Patrick as middle and Harris as last name. So firstName char array contains Neil, middleName char array contains Patrick and lastName char array contains Harris.

Next the fullName array is declared to store Neil Patrick Harris in a specific format:

strcpy(fullName, lastName);

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

strcat(fullName, ", ");

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

strcat(fullName, firstName);

This appends firstName to fullName which means Neil is appended to fullName which now contains Harris, Neil

strcat(fullName, " ");

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

char temp[2];

temp[0] = middleName[0];

This creates an array to hold the first initial of middle name i.e. P of Patrick

strcat(fullName, temp);

This appends an the first initial of middleName to fullName which means fullName now contains Harris, Neil P

strcat(fullName, ".");

This appends a period to fullName which means fullName now contains Harris, Neil P.

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

Full name is: Harris, Neil P.

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

Write a program that asks for the user's first, middle, and last name as one input-example-1
User Ilya Lysenko
by
7.9k points