Reverse a String in C++ using For loop
ADVERTISEMENTS
Reverse a string in C++ using for loop. In this article, you will learn how to reverse a string in C++ using for loop.
String Conversion
"abcd" after reverse ==> "dcba"
Source Code
// Reverse a String in C++ using For loop
#include <iostream>
using namespace std;
int main() {
char x[100], y[100];
int b, e, c = 0;
// b - To store the begin character
// e - To store the end character
// c - To store the string count
cout << "Enter a string to make reverse::\n";
cin >> x;
// calculation of the string length
while (x[c] != '\0') {
c++;
}
e = c - 1;
for (b = 0; b < c; b++) {
y[b] = x[e];
e--;
}
y[b] = '\0';
cout << "The reverse of the string is : " << y << "\n";
return 0;
}
Output
Enter a string to make reverse::
example
The reverse of the string is : elpmaxe