Open In App

Reverse a String – Complete Tutorial

Last Updated : 29 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a string s, the task is to reverse the string. Reversing a string means rearranging the characters such that the first character becomes the last, the second character becomes second last and so on.

Examples:

Input: s = “GeeksforGeeks”
Output: “skeeGrofskeeG”
Explanation : The first character G moves to last position, the second character e moves to second-last and so on.

Input: s = “abdcfe”
Output: “efcdba”
Explanation: The first character a moves to last position, the second character b moves to second-last and so on.

Using backward traversal – O(n) Time and O(n) Space

The idea is to start at the last character of the string and move backward, appending each character to a new string res. This new string res will contain the characters of the original string in reverse order.

C++
// C++ program to reverse a string using backward traversal

#include <iostream>
#include <string>
using namespace std;

string reverseString(string& s) {
    string res;
  
  	// Traverse on s in backward direction
  	// and add each charecter to a new string
    for (int i = s.size() - 1; i >= 0; i--) {
        res += s[i];
    }
    return res;
}

int main() {
    string s = "abdcfe";
    string res = reverseString(s);
    cout << res;
    return 0;
}
C Java Python C# JavaScript

Output
efcdba

Time Complexity: O(n) for backward traversal
Auxiliary Space: O(n) for storing the reversed string.

Using Two Pointers – O(n) Time and O(n) Space

The idea is to maintain two pointers: left and right, such that left points to the beginning of the string and right points to the end of the string.

While left pointer is less than the right pointer, swap the characters at these two positions. After each swap, increment the left pointer and decrement the right pointer to move towards the center of the string. This will swap all the characters in the first half with their corresponding character in the second half.

Illustration:



C++
// C++ program to reverse a string using two pointers

#include <bits/stdc++.h>
using namespace std;

string reverseString(string &s) {
    int left = 0, right = s.length() - 1;

    // Swap characters from both ends till we reach
    // the middle of the string
    while (left < right) {
        swap(s[left], s[right]);
        left++;
        right--;
    }
  
    return s;
}

int main() {
    string s = "abdcfe";
    cout << reverseString(s);
    return 0;
}
C Java Python C# JavaScript

Output
efcdba

Time Complexity: O(n) 
Auxiliary Space: O(n)

Using Recursion – O(n) Time and O(n) Space

The idea is to use recursion and define a recursive function that takes a string as input and reverses it. Inside the recursive function,

  • Swap the first and last element.
  • Recursively call the function with the remaining substring.
C++
// C++ Program to reverse an array using Recursion

#include <iostream>
#include <vector>
using namespace std;

// recursive function to reverse a string from l to r
void reverseStringRec(string &s, int l, int r) {
  
    // If the substring is empty, return
    if(l >= r)
        return;
  
    swap(s[l], s[r]);
  	
    // Recur for the remaining string
    reverseStringRec(s, l + 1, r - 1);
}

// function to reverse a string
string reverseString(string &s) {
    int n = s.length();
    reverseStringRec(s, 0, n - 1);
  	return s;
}

int main() {
    string s = "abdcfe";
  	cout << reverseString(s) << endl;
    return 0;
}
C Java Python C# JavaScript

Output
efcdba

Time Complexity: O(n) where n is length of string
Auxiliary Space: O(n)

Using Stack – O(n) Time and O(n) Space

The idea is to use stack for reversing a string because Stack follows Last In First Out (LIFO) principle. This means the last character you add is the first one you’ll take out. So, when we push all the characters of a string into the stack, the last character becomes the first one to pop.

Illustration:


C++
// C++ program to reverse a string using stack

#include <bits/stdc++.h>
using namespace std;

string reverseString(string &s) {
    stack<char> st;
  
    // Push the charcters into stack
    for (int i = 0; i < s.size(); i++) {
        st.push(s[i]);
    }

    // Pop the characters of stack into the original string
    for (int i = 0; i < s.size(); i++) {
        s[i] = st.top();
        st.pop();
    }
  
  	return s;
}

int main() {
    string s = "abdcfe";
    cout << reverseString(s);
    return 0;
}
Java Python C# JavaScript

Output
efcdba

Time Complexity: O(n) 
Auxiliary Space: O(n)

Using Inbuilt methods – O(n) Time and O(1) Space

The idea is to use built-in reverse method to reverse the string. If built-in method for string reversal does not exist, then convert string to array or list and use their built-in method for reverse. Then convert it back to string.

C++
#include <bits/stdc++.h>
using namespace std;

string reverseString(string &s) {
    reverse(s.begin(), s.end());
  	return s;
}

int main() {
    string s = "abdcfe"; 
    cout << reverseString(s) ;
    return 0;
} 
Java Python C# JavaScript

Output
efcdba

Time Complexity: O(n)
Auxiliary Space: O(1) in C++ and python and O(n) in Java, C# and JavaScript (extra space is used to store in array or list or StringBuilder for reversal).



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg