Reverse a String in C++

Video Tutorial
FREE
Reverse a string thumbnail
This video belongs to
Data Structures in C++ Course
13 modules
Certificate
Topics Covered

Introduction

There are multiple scenarios where you might want to reverse a string in c++, whether it be to simply print it in the reversed form or to simply reverse the string for further manipulations such as checking for palindromes, etc.

string-reversing-function

Different Methods to Reverse a String in C++ are:

  1. Using the reverse() function
  2. Using strrev()
  3. Creating your own function to reverse a string.

Let's look at how to reverse a string in c++.

Using reverse() Function in C++

The reverse() function is a built-in function in the c++ language. To use this function you must import the algorithm header file.

This reverse() function in the algorithm header file takes in 2 parameters as input the bidirectional begin and bidirectional end iterators to the string. A bidirectional iterator is essentially an iterator that has the features of a forward iterator and supports the decrement operators : prefix and postfix.

To access elements in both directions, i.e. towards the beginning and towards the end, we use the bidirectional iterators. Some of the containers in the C++ that implement these bidirectional iterators are list, set, map, etc.

Using functions like begin(), end() etc on a string return an iterator (something that can be iterated / looped over).

So, to reverse our string, we will pass on these iterators word.begin() and word.end() of the string to the reverse function and reverse our string.

Let's see how it is done :

Output :

And here we have our reversed string as output.


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.

Output:

Reverse Using Strrev reverse string in C++ 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

Now we know how to reverse a string, however, if you simply want to print the string in reverse, you can do so by looping over the string in reverse and printing every element.

Code :

Output :

Making our own string reversing function

We have seen 2 ways that you can use to reverse a string, but now, let's create our function to reverse a string.

If you carefully look at the reversed string along with the original string, like this :

you can see that the reversed string is the original string with letters swapped. How ? The first letter of the word s, is simply swapped with the last letter of the word r. Similarly, the second letter c, is swapped with the second last letter e, and so on.

Hence, if we would like to create a function of our own, we can simply swap the first half of the letters of the word with the second half of the letters. Here, in this approach, we will make changes in the original string itself to prevent the creation of a new string.

Let's visualize this function once :

string-reversing-function

As you can see in the GIF above, we have simply swapped the first half of the string. Since this string was of length 6, we have only swapped 3 times.

So here's how we would do it in code, we'd traverse only half the string (since we only have to swap the first half with the second half) and swap every element with its counterpart. This swapping is done by reference.

Code :

Output :

How to Reverse a Constant String?

Using Stack Data Structure

Output relacS

Complexity Analysis:

Time Complexity: O(N)

Auxiliary Space: O(N)

Using Vector Data Structure

Output relacS

Complexity Analysis:

Time Complexity: O(N)

Auxiliary Space: O(N)

Conclusion

  • There are 3 different ways to reverse a string in c++ :**
    • Using the reverse() function
    • Using strrev()
    • Creating your own function to reverse a string.
  • You can also print a string in reverse by looping over it from the reverse end.