C++ Program to reverse a string using do while
Program
#include <iostream>
#define MAX_SIZE 100
using namespace std;
int main()
{
char inputString[MAX_SIZE], reverseString[MAX_SIZE];
int strIndex, revIndex, length;
length = 0;
cout << "Enter any string: ";
cin >> inputString;
while(inputString[length] != '\0')
{
length++;
}
revIndex = 0;
strIndex = length - 1;
do {
reverseString[revIndex] = inputString[strIndex];
strIndex--;
revIndex++;
}while(strIndex >= 0);
reverseString[revIndex] = '\0';
cout << "Original string: " << inputString << endl;
cout << "Reversed string: " << reverseString;
return 0;
}
Output
$ g++ reverse-string-using-do-while.cpp
$ ./a.out
Enter any string: himalayas
Original string: himalayas
Reversed string: sayalamih