Trusted answers to developer questions

How to reverse a string in C++

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

In C++, reversing a string, or completely flipping a string means flipping changing the order of the characters in it, such that it reads backward.

The illustration below shows what happens when we reverse a string.

How do you reverse a string?

There are multiple implementations that can be used to reverse a string, depending on the requirements of the programme you are writing. Let’s look at all of them in order.

1. Using the built-in reverse function

C++ has an in-built reverse function, that can be called to reverse a string. This function takes in two inputs;

  • The iterator for the string start
  • The iterator for string ends

The code snippet below shows how to use this function:

#include <iostream>
//The library below must be included for the reverse function to work
#include<bits/stdc++.h>
using namespace std;
int main() {
string greeting = "Hello";
//Note that it takes the iterators to the start and end of the string as arguments
reverse(greeting.begin(),greeting.end());
cout<<greeting<<endl;
}

2. Using a loop

Within the main body of the function, a loop can be written to reverse a string.

In the loop body, you will use the swap function, which is in-built, to change element positions. In case one is not allowed to use any in-built function, the second code tab shows how to carry out the reverse loop.

The third tab shows how to do the same reversal with a while loop using the swap method.

#include <iostream>
using namespace std;
int main() {
string greeting = "Hello";
int len = greeting.length();
int n=len-1;
for(int i=0;i<(len/2);i++){
//Using the swap method to switch values at each index
swap(greeting[i],greeting[n]);
n = n-1;
}
cout<<greeting<<endl;
}

Note: The for loop only runs for half the length of the string as swaps are happening between the first and last positions and slowly moving inwards till both i and n are the same. The while loop shows this condition more explicitly.

3. Using a function

A function can also be written to reverse a string using recursion.

The code snippet below shows how:

#include <iostream>
using namespace std;
void reverse_String(string& greet, int n,int i){
if(n<=i){return;}
swap(greet[i],greet[n]);
reverse_String(greet,n-1,i+1);
}
int main() {
string greeting = "Hello";
cout<<"String before reversal: "<<greeting<<endl;
reverse_String(greeting,greeting.length()-1,0);
cout<<"String after reversal: "<<greeting<<endl;
}

4. Creating a new string

A roundabout method for reversing a string is looping backwards through it and storing the elements in a new string of the same size.

To add elements to an empty string, we can the push_back method.

The code below shows how:

#include <iostream>
using namespace std;
int main() {
string greeting = "Hello";
string new_greeting;
for(int n = greeting.length()-1; n >= 0; n--){
new_greeting.push_back(greeting[n]);
}
cout<<"Original string: "<< greeting << endl;
cout<<"New reversed string: "<< new_greeting << endl;
}

RELATED TAGS

reverse
string
c++
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?