📜  在不使用任何临时变量的情况下反转字符串

📅  最后修改于: 2022-05-13 01:57:07.743000             🧑  作者: Mango

在不使用任何临时变量的情况下反转字符串

我们得到一个字符串。我们还给出了字符串中第一个和最后一个字符的索引。任务是在不使用任何额外变量的情况下反转字符串。

例子:

Input  : str = "abc"
Output : str = "cba" 

Input :  str = "GeeksforGeeks"
Output : str = "skeeGrofskeeG"

如果我们看一下反转字符串或数组的程序,我们需要做的就是交换两个字符。这个想法是使用 XOR 来交换变量。下面是这个想法的实现。

C++
// C++ Program to reverse a string without
// using temp variable
#include 
using namespace std;
 
// Function to reverse string and return reversed 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;
}


Java
// Java Program to reverse a string without
// using temp variable
import java.util.*;
 
class GFG
{
 
// Function to reverse string and
// return reversed 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


Python3
# 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


C#
// C# Program to reverse a string without
// using temp variable
using System;
     
class GFG
{
 
// Function to reverse string and
// return reversed 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


Javascript


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


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


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


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


Javascript


输出:

skeeGrofskeeG

如果我们允许库函数,我们也可以使用在 C++ 中快速反转字符串中讨论的想法。我们甚至不需要第一个和最后一个字符的索引。

C++

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

Java

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

Python3

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

C#

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

Javascript


输出:

skeegrofskeeg
 

https://www.youtube.com/watch?v=Y

-UR3ravjRE