Reverse String using inbuilt method

  1. Use the inbuilt reverse() method available for strings.
  2. Convert the string to a list.
  3. Call the reverse() method on the list.
  4. Convert the list back to a string.

Below is the implementation to reverse a string using built-in methods in different programming languages:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string str = "w3wiki"; // Input string
    reverse(str.begin(),str.end()); // Reverse the string
    cout << str << std::endl;
    return 0;
}
 
 
// This code is contributed by prajwalkandekar123.


Java




//Java program to reverse a string using StringBuffer class
 
import java.io.*;
import java.util.*;
 
class GFG {
     
    //Driver Code
    public static void main (String[] args) {
        String str = "w3wiki";//Input String
         
        //Step 1: Initialise an object of StringBuffer class
        StringBuffer sb = new StringBuffer(str);
         
        //Step 2: Invoke the .reverse() method
        sb.reverse();
         
        //Step 3: Convert the StringBuffer to string by using toString() method
        System.out.println(sb.toString());
         
    }
}
 
//This code is contributed by shruti456rawal


Python3




# Driver Code
if __name__ == '__main__':
    str = "w3wiki" # Input String
 
    # Step 1: Initialise an object of StringBuffer class
    sb = str[::-1]
 
    # Step 2: Invoke the .reverse() method (not applicable in Python)
 
    # Step 3: Print the reversed string
    print(sb)


C#




// c# code
 
using System;
 
class Program {
    static void Main(string[] args)
    {
        string str = "w3wiki"; // Input string
        char[] charArray
            = str.ToCharArray(); // Convert string to char
                                 // array
        Array.Reverse(charArray); // Reverse the array
        str = new string(
            charArray); // Convert char array back to string
        Console.WriteLine(
            str); // Output the reversed string
    }
}
// ksam24000


Javascript




let str = "w3wiki"; // Input string
str = str.split('').reverse().join(''); // Reverse the string
console.log(str);
 
// This code is contributed by Prajwal Kandekar


Output

skeegrofskeeg

Time Complexity: O(n)
Auxiliary Space: O(1) in all the programming languages except Java in which it will be, O(n) (The extra space is used to store the StringBuffer string).

String Reverse in C/C++/Java/Python/JavaScript

String reverse or reverse a string means changing the position of each character of the given string to its opposite position from end, i.e. if a character is at position 1 then its new position will be String.length, similarly if a character is at position 2 then its new position will be String.length – 1, and so on.

String Reverse in C/C++/Java/Python/JavaScript

Given a string, write a program to reverse the string.

Input: original_string[] = Geeks
Output: string_reversed[] = skeeG

Input: original_string[] = w3wiki
Output: string_reversed[] = skeeGroFskeeG

Table of Content

  • Reverse String using a Loop:
  • Reverse String using inbuilt method
  • Reverse String using Recursion:
  • Reverse String using two pointers:
  • String Reverse String using Stack:

Recommended Problem
Reverse a String
Solve Problem

Similar Reads

Reverse String using a Loop:

Initialize an empty string to store the reversed result. Iterate through the original string in reverse order. Append each character to the new string. The new string is the reversed version of the original string....

Reverse String using inbuilt method

...

Reverse String using Recursion:

...

Reverse String using two pointers:

...

String Reverse String using Stack:

...