w3resource

C++ Exercises: Accepts the user's first and last name and print them in reverse order with a space between them

C++ Basic: Exercise-42 with Solution

Reverse First and Last Name

Write a C++ program that accepts the user's first and last name and prints them in reverse order with a space between them.

Visual Presentation:

C++ Exercises: Accepts the user's first and last name and print them in reverse order with a space between them

Sample Solution:

C++ Code :

# include <iostream> // Including the input-output stream header file
# include <string> // Including the string header file to work with strings
using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    char fname[30], lname[30]; // Declaring character arrays to store first and last names

    cout << "\n\n Print the name in reverse where last name comes first:\n"; // Outputting a message indicating the purpose of the program
    cout << "-----------------------------------------------------------\n"; // Displaying a separator line

    cout << " Input First Name: "; // Prompting the user to input the first name
    cin >> fname; // Reading the first name from the user

    cout << " Input Last Name: "; // Prompting the user to input the last name
    cin >> lname; // Reading the last name from the user

    // Outputting the reversed name where the last name comes first
    cout << " Name in reverse is: " << lname << " " << fname << endl; // Displaying the reversed name

    cout << endl; // Displaying an empty line for better readability
    return 0; // Return statement indicating successful completion of the program
}

Sample Output:

 Print the name in reverse where last name comes first:                
-----------------------------------------------------------            
 Input First Name: Alexandra                                           
 Input Last Name: Abramov                                              
 Name in reverse is: Abramov Alexandra 

Flowchart:

Flowchart: Accepts the user's first and last name and print them in reverse order with a space between them

C++ Code Editor:

Previous: Write a program in C++ to print an American flag on the screen.
Next: Write a language program which accepts the radius of a circle from the user and compute the area and circumference.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/basic/cpp-basic-exercise-42.php