The Program in C++ Program to Reverse a Given String is given below:
#include <iostream>
#include <string>
int main() {
std::string input_string;
std::cout << "Hello Codeauri Family, Enter a string here to reverse it: ";
std::getline(std::cin, input_string);
std::cout << "Well,the reverse of the given string is: ";
for (int i = input_string.length() - 1; i >= 0; i--) {
std::cout << input_string[i];
}
return 0;
}
Output:
Hello Codeauri Family, Enter a string here to reverse it: smile please
Well,the reverse of the given string is: esaelp elims
Pro-Tips💡
This program takes a string as input from the user using std::getline
and stores it in a std::string
variable named input_string
.
Then, it uses a for
loop to iterate through the string in reverse and print each character.
The loop starts from the last index of the string and decrements the index until it reaches 0.
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.