Reverse string without using any temporary variable
Last Updated :
07 Jul, 2022
We are given a string. We are also given indexes of first and last characters in string. The task is to reverse the string without using any extra variable.
Examples:
Input : str = "abc"
Output : str = "cba"
Input : str = "GeeksforGeeks"
Output : str = "skeeGrofskeeG"
If we take a look at program to reverse a string or array, all we need to do is swap two characters. The idea is to use XOR for swapping the variable. Below is the implementation of the idea.
C++
#include <bits/stdc++.h>
using namespace std;
string reversingString(string str, int start, int end)
{
while (start < end)
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
++start;
--end;
}
return str;
}
int main()
{
string s = "GeeksforGeeks" ;
cout << reversingString(s, 0, 12);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String reversingString( char []str,
int start,
int end)
{
while (start < end)
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
++start;
--end;
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String s = "GeeksforGeeks" ;
System.out.println(reversingString
(s.toCharArray(), 0 , 12 ));
}
}
|
Python3
def reversingString( str , start, end):
while (start < end):
str = ( str [:start] + chr ( ord ( str [start]) ^
ord ( str [end])) +
str [start + 1 :]);
str = ( str [:end] + chr ( ord ( str [start]) ^
ord ( str [end])) +
str [end + 1 :]);
str = ( str [:start] + chr ( ord ( str [start]) ^
ord ( str [end])) +
str [start + 1 :]);
start + = 1 ;
end - = 1 ;
return str ;
s = "GeeksforGeeks" ;
print (reversingString(s, 0 , 12 ));
|
C#
using System;
class GFG
{
static String reversingString( char []str,
int start,
int end)
{
while (start < end)
{
str[start] ^= str[end];
str[end] ^= str[start];
str[start] ^= str[end];
++start;
--end;
}
return String.Join( "" , str);
}
public static void Main(String[] args)
{
String s = "GeeksforGeeks" ;
Console.WriteLine(reversingString
(s.ToCharArray(), 0, 12));
}
}
|
Javascript
<script>
function reversingString(str, start, end)
{
while (start < end)
{
str[start] = String.fromCharCode(
str[start].charCodeAt(0) ^
str[end].charCodeAt(0));
str[end] = String.fromCharCode(
str[end].charCodeAt(0) ^
str[start].charCodeAt(0));
str[start] = String.fromCharCode(
str[start].charCodeAt(0) ^
str[end].charCodeAt(0));
++start;
--end;
}
return (str).join( "" );
}
let s = "GeeksforGeeks" ;
document.write(reversingString(
s.split( "" ), 0, 12));
</script>
|
Output:
skeeGrofskeeG
Time Complexity: O(n)
Auxiliary Space: O(1)
If we are allowed to library function, we can also use the idea discussed in quickly reverse a string in C++. We don’t even need indexes of first and last characters.
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str = "geeksforgeeks" ;
reverse(str.begin(), str.end());
cout << str;
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
StringBuilder str = new StringBuilder( "geeksforgeeks" );
str.reverse();
System.out.println(str);
}
}
|
Python3
str = "geeksforgeeks" ;
str = "".join( reversed ( str ))
print ( str );
|
C#
using System;
using System.Linq;
class GFG
{
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
str = new string (str.Reverse().ToArray());
Console.WriteLine(str);
}
}
|
Javascript
<script>
function reverseString(str) {
return str.split( "" ).reverse().join( "" );
}
var str = ( "geeksforgeeks" );
document.write(reverseString(str));
</script>
|
Output:
skeegrofskeeg
Time complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Aarti_Rathi and Mr. Somesh Awasthi.
Share your thoughts in the comments
Please Login to comment...