This is a C++ Program to Reverse a String.
The program takes a string and reverses it.
1. The program takes a string.
2. Using a for loop and string function, the string is reversed.
3. The result is printed.
Here is the source code of C++ Program to Reverse a String. The program output is shown below.
using namespace std;
int main ()
char str, temp;
int i, j;
cout << "Enter a string : ";
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
temp = str[i];
str[i] = str[j];
str[j] = temp;
cout << "\nReverse string : " << str;
1. The user is asked to enter a string and it is stored in the character variable ‘str’.
2. The length of ‘str’ is stored in the variable ‘j’ and ‘i’ is initialized as 0.
3. Using a for loop, the string is reversed.
4. The ith character of str is swapped with jth character using a temporary variable ‘temp’.
5. The loop terminates when i is less than j.
6. str is then printed which is th result.
Case 1 : Enter a string : MALAYALAM Reverse string : MALAYALAM Case 2 : Enter a string : gubed Reverse string : debug Case 3 : Enter a string : CruisE Reverse string : EsiurC
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Programming Books
- Buy C++ Books
- Practice Programming MCQs
- Apply for Information Technology Internship
- Apply for C++ Internship