Easiest Way to Reverse a String
Last Updated :
05 Feb, 2024
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:
- String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder. After that, print out the characters of the reversed string by scanning from the first till the last index.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "geeksforgeeks" ;
reverse(str.begin(), str.end());
cout << str;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class ReverseString {
public static void main(String[] args)
{
String input = "geeksforgeeks";
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
System.out.println(input1);
}
}
|
Python3
def reverse_string(string):
return string[:: - 1 ]
str = "geeksforgeeks"
reversed_str = reverse_string( str )
print (reversed_str)
|
C#
using System;
using System.Text;
class ReverseString
{
public static void Main( string [] args)
{
string input = "geeksforgeeks" ;
StringBuilder input1 = new StringBuilder();
input1.Append(input);
input1 = ReverseStringBuilder(input1);
Console.WriteLine(input1);
}
public static StringBuilder ReverseStringBuilder(StringBuilder sb)
{
char [] arr = sb.ToString().ToCharArray();
Array.Reverse(arr);
return new StringBuilder( new string (arr));
}
}
|
Javascript
function reverseString(str) {
let input1 = new StringBuilder();
input1.append(str);
input1.reverse();
return input1.toString();
}
class StringBuilder {
constructor() {
this .strings = [];
}
append(str) {
this .strings.push(str);
}
reverse() {
this .strings = this .strings.join( '' ).split( '' ).reverse();
}
toString() {
return this .strings.join( '' );
}
}
function main() {
let input = "geeksforgeeks" ;
let reversedString = reverseString(input);
console.log(reversedString);
}
main();
|
Complexity Analysis:
Time Complexity: O(N), N is the size of the string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...