C++ Program To Reverse A String Using While Loop

In today’s post we will try to explore that how we can reverse a string in C++.

Reversing a string means reversing the position of all characters of that string.

For example reverse of “HELLO” is “OLLEH” and reverse of “WORLD” is “DLROW”.

The are some steps required to find the reverse of a string.

  1. Read the whole string.

  2. Interchange the first character (i) with last character (j).

  3. Interchange the second character (i+1) with second last character (j-1).

  4. Go on interchanging the characters until interchanging characters meet each other (i=j).

C++ Code To Reverse A String:

using namespace std;
int main()
	char str[50],temp;
	int first_character=0;
	int last_character;
	cout<<"Enter The String: ";
	cout<<"Reversed String: "<<str<<endl;
	return 0;

Program Output:

Program To Reverse A String in C++

Source Code Explanation:

First of all, we declared a C-style string named str with a limit of 50 characters.

Then we declared one more char type variable named temp. This variable will help use to interchange the characters occurring at different positions.

After that, we declared 2 variables of integer type. Names of both variables are self-explanatory. first_character is initialized to zero because it will point to the first character of string and an array always starts with zero index.

Then we prompted the user to enter desired string and read it through gets() function.

Next, we are interested in finding the length of our string and pointing last_character to the actual last character of string. We found the length of string using function strlen() and subtracted 1 from length.

The reason behind subtracting from length is that a C-style string always ends with null character (\0) and stores this element at its last index. We want to point last_character to the actual last character so we subtracted to eliminate the null character.

Next we used a while loop and set a condition of i<j. When this condition becomes false i.e. i=j then this loop is terminated.

Above I have described that we are going to interchange characters until i<j and we will stop if i becomes equal to j.

In loop body, we are going to interchange the characters at position  first_character and last_character.

At start,  first_character is at and last_character is at length-1.

After reversing for once we incremented first_character and decremented  last_character.

In this way, they will point to the characters occurring next to them.

Basically swapping technique is used to interchange the characters. I highly recommend you to read this article about swapping technique.

At the end, we printed the reverse of string on screen.

Dry Run:

At Start first_character=0 last_character=4


When first_character=1 last_character=3


When first_character=2 last_character=2

Loop will not execute because condition (i<j) c=becomes false.

So character at this position remains same i.e. str[2]=l



Now, we can see that our str contains:






So our original string “Hello” has been reversed to “olleH”.

If you liked this article, then I would really appreciate if you follow us on Twitter and Facebook.

If you have any question then you can ask me in comments. I would like to answer them.

You can also Click Here To Download Exercise Files.