Open In App

Easiest Way to Reverse a String

Have you wondered which is the easiest way to reverse a string, Imagine you are giving a contest and an algorithm you have made requires reversing a string, you must be aware of the easiest way to reverse a string.

What is a reversal of string:

Reversing a string is the technique that reverses or changes the order of a given string so that the last character of the string becomes the first character , second last becomes second character of the string and so on.



Examples:

Input: “ABC”
Output: “CBA”



Input: “geeksforgeeks”
Output: “skeegrofskeeg”

The easiest way to reverse a string:

The easiest way to reverse a string is to use the inbuilt function of respective languages. There is a direct function in the “algorithm” header file for doing reverse that saves our time when programming.

// Reverses elements in [begin, end]
void reverse (BidirectionalIterator begin, BidirectionalIterator end);

What is reverse() function in C++:

reverse() is a predefined function in the header file algorithm. It is defined as a template in the above-mentioned header file. It reverses the order of the elements in the range [first, last) of any container. The time complexity is O(n). 

Note: The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

Syntax: 

void reverse(BidirectionalIterator first, BidirectionalIterator last)
Explanation:
BidirectionalIterator is an iterator that can be used to access any
elements of a container in both forward and backward direction.

Built in reverse() method of the StringBuilder class in Java: 

Below is the Implementation of the above approach:




// C++ program to illustrate the
// reversing of a string using
// reverse() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str = "geeksforgeeks";
 
    // Reverse str[begin..end]
    reverse(str.begin(), str.end());
 
    cout << str;
    return 0;
}




// Java program to ReverseString using StringBuilder
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Class of ReverseString
class ReverseString {
    public static void main(String[] args)
    {
        String input = "geeksforgeeks";
 
        StringBuilder input1 = new StringBuilder();
 
        // append a string into StringBuilder input1
        input1.append(input);
 
        // reverse StringBuilder input1
        input1.reverse();
 
        // print reversed String
        System.out.println(input1);
    }
}




# Python program to reverse a string
 
def reverse_string(string):
    return string[::-1]
 
str = "geeksforgeeks"
reversed_str = reverse_string(str)
print(reversed_str)
 
# This code is contributed by Sakshi




using System;
using System.Text;
 
class ReverseString
{
    public static void Main(string[] args)
    {
        string input = "geeksforgeeks";
 
        StringBuilder input1 = new StringBuilder();
 
        // append a string into StringBuilder input1
        input1.Append(input);
 
        // reverse StringBuilder input1
        input1 = ReverseStringBuilder(input1);
 
        // print reversed String
        Console.WriteLine(input1);
    }
 
    // Method to reverse a StringBuilder
    public static StringBuilder ReverseStringBuilder(StringBuilder sb)
    {
        char[] arr = sb.ToString().ToCharArray();
        Array.Reverse(arr);
        return new StringBuilder(new string(arr));
    }
}




// JavaScript program to reverse a string using StringBuilder
 
// Function to reverse a string
function reverseString(str) {
    // Create a StringBuilder equivalent in JavaScript
    let input1 = new StringBuilder();
 
    // Append the input string to the StringBuilder
    input1.append(str);
 
    // Reverse the StringBuilder
    input1.reverse();
 
    // Return the reversed string
    return input1.toString();
}
 
// Class equivalent to StringBuilder in JavaScript
class StringBuilder {
    constructor() {
        this.strings = [];
    }
 
    // Append a string to the StringBuilder
    append(str) {
        this.strings.push(str);
    }
 
    // Reverse the StringBuilder
    reverse() {
        this.strings = this.strings.join('').split('').reverse();
    }
 
    // Convert the StringBuilder to a string
    toString() {
        return this.strings.join('');
    }
}
 
// Main function
function main() {
    // Input string
    let input = "geeksforgeeks";
 
    // Call the reverseString function
    let reversedString = reverseString(input);
 
    // Print the reversed string
    console.log(reversedString);
}
 
// Call the main function
main();

Output
skeegrofskeeg

Complexity Analysis:

Time Complexity: O(N), N is the size of the string.
Auxiliary Space: O(1)


Article Tags :