The Program in C++ that accepts the User’s first and last name and print them in reverse order is given below:
#include <iostream>
#include <string>
int main() {
std::string firstName, lastName;
std::cout << "Enter your first name: ";
std::getline(std::cin, firstName);
std::cout << "Enter your last name: ";
std::getline(std::cin, lastName);
std::cout << "Your name in reverse order is: ";
for (int i = lastName.length() - 1; i >= 0; i--) {
std::cout << lastName[i];
}
std::cout << " ";
for (int i = firstName.length() - 1; i >= 0; i--) {
std::cout << firstName[i];
}
return 0;
}
Output:
Enter your first name: kind
Enter your last name: heart
Your name in reverse order is: traeh dnik
Pro-Tips💡
The program uses the getline
function to read the user’s first and last name,
which allows the user to enter a name with spaces.
Then it uses a for loop to iterate through the characters in the name strings and print them in reverse order.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.