Name Arranger using C++
Name Arranger
Write a C++ program that asks for the user’s first, middle, and last names. The names
should 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 middle
name. 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.
Answer:
// Name Arranger #include <iostream> #include <cstring> using namespace std; // Constants for the name sizes const int SIZE = 21; const int FULL_SIZE = 63; int main() { char first[SIZE]; // To hold the first name char middle[SIZE]; // To hold the middle name char last[SIZE]; // To hold the last name char full[FULL_SIZE]; // To hold the full name // Get the first name. cout << "Enter your first name: "; cin.getline(first, SIZE); // Get the middle name. cout << "Enter your middle name: "; cin.getline(middle, SIZE); // Get the last name cout << "Enter your last name: "; cin.getline(last, SIZE); // Copy the names to the full array. strcpy(full, last); strcat(full, ", "); strcat(full, first); strcat(full, " "); strcat(full, middle); // Display the full name. cout << full << endl; return 0; }
Leave a reply