Reverse String in C++ With Examples

In many situations, we may need to reverse a string in C++ programming. It may include just printing a reversed string. Or maybe in some cases need to reverse the string permanently at its address.

In this tutorial, we are going to learn how we can accomplish both the tasks. That too using different pre-defined as well as user-defined functions.

Reversing a String in C++

Reversing a string refers to the operation on a string, by which the sequence of characters in it gets reversed. For example, consider ‘str’ contains a string “JournalDev” in it.

After the reversal operation takes place on the string ‘str’, the content should get reversed. Hence now, ‘str’ should contain the string “veDlanruoJ”.

Now let us see how we can perform this reverse operation on C++ strings using various techniques.

Using reverse() function in C++

The built-in reverse function reverse() in C++ directly reverses a string. Given that both bidirectional begin and end iterators are passed as arguments.

This function is defined in the algorithm header file. The code given below describes the use of reverse() function,



Use Of reverse()

Using strrev()

strrev() is a pre-defined function in C++, defined inside the cstring.h header file. It is extensively applicable for reversing any C-string(character array).

Further, it only requires the base address of the string as its argument and reverses the string accordingly. Let us see how we can use the strrev() function in C++ to reverse strings.



Reverse Using strrev()

The code above illustrates the working of the function strrev() very well. For a string ‘str’, the function reverses it successfully as we can see in the output itself.

Printing a String in reverse

In certain cases, we may not need to change the string but only print it in a reversed manner. This could be for constant strings that cannot be modified. We can print any string in a reversed pattern by using a loop. Let us see how.



Reverse Print
  • In the above-given code, we have firstly initialized a string ‘str’.
  • Inside the for loop, while printing the string, note that we have initialized the iterator ‘i’ with a value str.length()-1. This means that we print the string character by character but, starting from the last index.
  • Note: length() returns the length of a string. So, for printing a string in reverse we should consider the last index which should be length()-1, since indexing starts from ‘0’ in a character array.

Making our own string reversing function My_rev()

Until now we learned how we can print a string in reverse as well as reverse the string using different pre-defined functions. Now let us create or define our own function named My_rev() to reverse a given string.



User-defined My_rev() function Output
  • In the code above, My_rev() is a function that reverses a string, given the base address of the string is passed as an argument.
  • Inside the My_rev() function, *str is a pointer that stores the base address of the provided string. In our case, str points to the first element of the string My_string.
  • len stores the length of the string. Whereas, n is the index of the last element.
  • In the function, we try to swap individual characters of the string, from both ends. That means we go on swapping elements from the 0th and nth index until we get to the (len/2)th position. In the code above, the for loop does this swapping for us which technically reverses the string.
  • In the end, we return the base address str to the main() function. Where the string is printed using the cout function.


By admin

Leave a Reply

%d bloggers like this: