Reverse string without using any temporary variable

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to reverse a string without
// using temp variable
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse string and return revesed string
string reversingString(string str, int start, int end)
{
    // Iterate loop upto start not equal to end
    while (start < end)
    {
        // XOR for swapping the variable
        str[start] ^= str[end];
        str[end] ^= str[start];
        str[start] ^= str[end];
  
        ++start;
        --end;
    }
    return str;
}
  
// Driver Code
int main()
{
    string s = "GeeksforGeeks";
    cout << reversingString(s, 0, 12);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to reverse a string without
// using temp variable
import java.util.*;
  
class GFG 
{
  
// Function to reverse string and
// return revesed string
static String reversingString(char []str,
                               int start, 
                               int end)
{
    // Iterate loop upto start not equal to end
    while (start < end)
    {
        // XOR for swapping the variable
        str[start] ^= str[end];
        str[end] ^= str[start];
        str[start] ^= str[end];
  
        ++start;
        --end;
    }
    return String.valueOf(str);
}
  
// Driver Code
public static void main(String[] args) 
{
    String s = "GeeksforGeeks";
    System.out.println(reversingString
                      (s.toCharArray(), 0, 12));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to reverse a string 
# without using temp variable
  
# Function to reverse string and 
# return reversed string
def reversingString(str, start, end):
      
    # Iterate loop upto start not equal to end
    while (start < end):
  
        # XOR for swapping the variable
        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;
  
# Driver Code
s = "GeeksforGeeks";
print(reversingString(s, 0, 12));
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to reverse a string without
// using temp variable
using System;
      
class GFG 
{
  
// Function to reverse string and
// return revesed string
static String reversingString(char []str,
                              int start, 
                              int end)
{
    // Iterate loop upto start 
    // not equal to end
    while (start < end)
    {
        // XOR for swapping the variable
        str[start] ^= str[end];
        str[end] ^= str[start];
        str[start] ^= str[end];
  
        ++start;
        --end;
    }
    return String.Join("", str);
}
  
// Driver Code
public static void Main(String[] args) 
{
    String s = "GeeksforGeeks";
    Console.WriteLine(reversingString
                     (s.ToCharArray(), 0, 12));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

 skeeGrofskeeG

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Reversing a string using reverse()
#include<bits/stdc++.h>
using namespace std;
  
int main()
{
   string str = "geeksforgeeks";
      
   // Reverse str[beign..end]
   reverse(str.begin(), str.end());
      
   cout << str;
   return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Reversing a string using reverse()
class GFG 
{
public static void main(String[] args)
{
    StringBuilder str = new StringBuilder("geeksforgeeks");
      
    // Reverse str[beign..end]
    str.reverse();
    System.out.println(str);
}
}
  
// This code is contributed 
// by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Reversing a string using reverse()
str = "geeksforgeeks";
      
# Reverse str[beign..end]
str = "".join(reversed(str))
      
print(str);
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Reversing a string using reverse()
using System;
using System.Linq;                 
  
class GFG 
{
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
      
    // Reverse str[beign..end]
    str = new string(str.Reverse().ToArray());
    Console.WriteLine(str);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

 skeegrofskeeg

This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : princiraj1992, 29AjayKumar



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.