给定一个字符串,编写一个递归程序来反转它。
方法1(使用堆栈)
C++
// C++ program to reverse a string using stack
#include
using namespace std;
void recursiveReverse(string &str)
{
stack st;
for (int i=0; i
Java
// Java program to reverse a string using stack
import java.util.*;
class GFG
{
public static String recursiveReverse(char []str)
{
Stack st = new Stack<>();
for(int i=0; i
Python3
# Python program to reverse a string using stack
def recursiveReverse(str):
# using as stack
stack = []
for i in range(len(str)):
stack.append(str[i])
for i in range(len(str)):
str[i] = stack.pop()
if __name__ == "__main__":
str = "geeksforgeeks"
# converting string to list
# because strings do not support
# item assignment
str = list(str)
recursiveReverse(str)
# converting list to string
str = ''.join(str)
print(str)
# This code is contributed by
# sanjeev2552
C#
// C# program to reverse a string using stack
using System;
using System.Collections.Generic;
class GFG
{
public static String recursiveReverse(char []str)
{
Stack st = new Stack();
for(int i = 0; i < str.Length; i++)
st.Push(str[i]);
for (int i = 0; i < str.Length; i++)
{
str[i] = st.Peek();
st.Pop();
}
// converting character array to string
return String.Join("",str);
}
// Driver program
public static void Main()
{
String str = "geeksforgeeks";
// passing character array as parameter
str = recursiveReverse(str.ToCharArray());
Console.WriteLine(str);
}
}
// This code is contributed by Rajput-Ji
Javascript
C++
// A Simple Iterative C++ program to reverse
// a string
#include
using namespace std;
// Function to reverse a string
void reverseStr(string& str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
// Driver program
int main()
{
string str = "geeksforgeeks";
reverseStr(str);
cout << str;
return 0;
}
Java
// A Simple Java program
// to reverse a string
import java.util.Scanner;
public class reverseStr
{
// Function to reverse
// a string
void stringReverse()
{
String str = "geeksforgeeks";
int length = str.length();
StringBuffer revString = new StringBuffer();
for (int i = length - 1;
i >= 0; i--)
{
revString.append(str.charAt(i));
}
System.out.println(revString);
}
// Driver Code
public static void main(String []args)
{
reverseStr s= new reverseStr();
s.stringReverse();
}
}
// This code is contributed
// by prabhat kumar singh
Python
# A Simple python program
# to reverse a string
# Function to
# reverse a string
def reverseStr(str):
n = len(str)
# initialising a empty
# string 'str1'
str1 = ''
i = n - 1
while i >= 0:
# copy str
# to str1
str1 += str[i]
i -= 1
print(str1)
# Driver Code
def main():
str = "geeksforgeeks";
reverseStr(str);
if __name__=="__main__":
main()
# This code is contributed
# by prabhat kumar singh
C#
// A Simple Iterative C# program to reverse
// a string
using System;
class GFG
{
// Function to reverse a string
static String reverseStr(String str)
{
int n = str.Length;
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
str = swap(str,i,n - i - 1);
return str;
}
static String swap(String str, int i, int j)
{
char []ch = str.ToCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return String.Join("",ch);
}
// Driver code
public static void Main(String[] args)
{
string str = "geeksforgeeks";
str= reverseStr(str);
Console.WriteLine(str);
}
}
// This code is contributed by Princi Singh
PHP
C++
// A Simple Iterative C++ program to reverse
// a string
#include
using namespace std;
// Function to reverse a string
void reverseStr(string& str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
Java
//A Simple Iterative Java program to reverse
//a string
class GFG {
//Function to reverse a string
static void reverseStr(String str)
{
int n = str.length();
char []ch = str.toCharArray();
char temp;
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
Python3
# A Simple Iterative Python program to
# reverse a string
# Function to reverse a string
def reverseStr(str):
n = len(str)
i, j = 0, n-1
# Swap character starting from
# two corners
while i < j:
str[i], str[j] = str[j], str[i]
i += 1
j -= 1
# Driver code
if __name__ == "__main__":
str = "geeksforgeeks"
# converting string to list
# because strings do not support
# item assignment
str = list(str)
reverseStr(str)
# converting list to string
str = ''.join(str)
print(str)
# This code is contributed by
# sanjeev2552
C#
// A Simple Iterative C# program
// to reverse a string
using System;
class GFG
{
//Function to reverse a string
static void reverseStr(String str)
{
int n = str.Length;
char []ch = str.ToCharArray();
char temp;
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
PHP
C++
// Recursive C++ program to reverse a string
#include
using namespace std;
void recursiveReverse(string &str, int i = 0)
{
int n = str.length();
if (i == n / 2)
return;
swap(str[i], str[n - i - 1]);
recursiveReverse(str, i + 1);
}
// Driver program
int main()
{
string str = "geeksforgeeks";
recursiveReverse(str);
cout << str;
return 0;
}
Java
// Recursive Java program to reverse a string
class GFG
{
static void recursiveReverse(char[] str, int i)
{
int n = str.length;
if (i == n / 2)
return;
swap(str,i,n - i - 1);
recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
char temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
// Driver program
public static void main(String[] args)
{
char[] str = "geeksforgeeks".toCharArray();
recursiveReverse(str,0);
System.out.println(String.valueOf(str));
}
}
// This code is contributed by 29AjayKumar
Python3
# Recursive Python program to reverse a string
def recursiveReverse(str, i = 0):
n = len(str)
if i == n // 2:
return
str[i], str[n-i-1] = str[n-i-1], str[i]
recursiveReverse(str, i+1)
if __name__ == "__main__":
str = "geeksforgeeks"
# converting string to list
# because strings do not support
# item assignment
str = list(str)
recursiveReverse(str)
# converting list to string
str = ''.join(str)
print(str)
# This code is contributed by
# sanjeev2552
C#
// Recursive C# program to reverse a string
using System;
public class GFG
{
static void recursiveReverse(char[] str, int i)
{
int n = str.Length;
if (i == n / 2)
return;
swap(str,i,n - i - 1);
recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
char temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
// Driver program
public static void Main(String[] args)
{
char[] str = "geeksforgeeks".ToCharArray();
recursiveReverse(str,0);
Console.WriteLine(String.Join("",str));
}
}
// This code is contributed by Princi Singh
C++
// A quickly written program for reversing a string
// using reverse()
#include
using namespace std;
int main()
{
string str = "geeksforgeeks";
// Reverse str[beign..end]
reverse(str.begin(),str.end());
cout << str;
return 0;
}
Java
// A Simple Java program
// to reverse a string
class GFG
{
public static void main(String[] args)
{
String str = "geeksforgeeks";
// Reverse str[beign..end]
str = reverse(str);
System.out.println(str);
}
static String reverse(String input)
{
char[] temparray = input.toCharArray();
int left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
}
// This code is contributed by 29AjayKumar
Python
# A Simple python program
# to reverse a string
# Function to
# reverse a string
def reverseStr(str):
# print the string
# from last
print(str[::-1])
# Driver Code
def main():
str = "geeksforgeeks";
reverseStr(str);
if __name__=="__main__":
main()
# This code is contributed
# by prabhat kumar singh
C#
// A Simple C# program to reverse a string
using System;
class GFG
{
public static void Main(String[] args)
{
String str = "geeksforgeeks";
// Reverse str[beign..end]
str = reverse(str);
Console.WriteLine(str);
}
static String reverse(String input)
{
char[] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join("",temparray);
}
}
/* This code is contributed by PrinciRaj1992 */
PHP
输出:
skeegrofskeeg
时间复杂度: O(n)
辅助空间: O(n)
方法2(迭代)
C++
// A Simple Iterative C++ program to reverse
// a string
#include
using namespace std;
// Function to reverse a string
void reverseStr(string& str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
// Driver program
int main()
{
string str = "geeksforgeeks";
reverseStr(str);
cout << str;
return 0;
}
Java
// A Simple Java program
// to reverse a string
import java.util.Scanner;
public class reverseStr
{
// Function to reverse
// a string
void stringReverse()
{
String str = "geeksforgeeks";
int length = str.length();
StringBuffer revString = new StringBuffer();
for (int i = length - 1;
i >= 0; i--)
{
revString.append(str.charAt(i));
}
System.out.println(revString);
}
// Driver Code
public static void main(String []args)
{
reverseStr s= new reverseStr();
s.stringReverse();
}
}
// This code is contributed
// by prabhat kumar singh
Python
# A Simple python program
# to reverse a string
# Function to
# reverse a string
def reverseStr(str):
n = len(str)
# initialising a empty
# string 'str1'
str1 = ''
i = n - 1
while i >= 0:
# copy str
# to str1
str1 += str[i]
i -= 1
print(str1)
# Driver Code
def main():
str = "geeksforgeeks";
reverseStr(str);
if __name__=="__main__":
main()
# This code is contributed
# by prabhat kumar singh
C#
// A Simple Iterative C# program to reverse
// a string
using System;
class GFG
{
// Function to reverse a string
static String reverseStr(String str)
{
int n = str.Length;
// Swap character starting from two
// corners
for (int i = 0; i < n / 2; i++)
str = swap(str,i,n - i - 1);
return str;
}
static String swap(String str, int i, int j)
{
char []ch = str.ToCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return String.Join("",ch);
}
// Driver code
public static void Main(String[] args)
{
string str = "geeksforgeeks";
str= reverseStr(str);
Console.WriteLine(str);
}
}
// This code is contributed by Princi Singh
的PHP
输出:
skeegrofskeeg
时间复杂度: O(n)
辅助空间: O(1)
方法3(使用两个指针迭代)
C++
// A Simple Iterative C++ program to reverse
// a string
#include
using namespace std;
// Function to reverse a string
void reverseStr(string& str)
{
int n = str.length();
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
Java
//A Simple Iterative Java program to reverse
//a string
class GFG {
//Function to reverse a string
static void reverseStr(String str)
{
int n = str.length();
char []ch = str.toCharArray();
char temp;
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
Python3
# A Simple Iterative Python program to
# reverse a string
# Function to reverse a string
def reverseStr(str):
n = len(str)
i, j = 0, n-1
# Swap character starting from
# two corners
while i < j:
str[i], str[j] = str[j], str[i]
i += 1
j -= 1
# Driver code
if __name__ == "__main__":
str = "geeksforgeeks"
# converting string to list
# because strings do not support
# item assignment
str = list(str)
reverseStr(str)
# converting list to string
str = ''.join(str)
print(str)
# This code is contributed by
# sanjeev2552
C#
// A Simple Iterative C# program
// to reverse a string
using System;
class GFG
{
//Function to reverse a string
static void reverseStr(String str)
{
int n = str.Length;
char []ch = str.ToCharArray();
char temp;
// Swap character starting from two
// corners
for (int i=0, j=n-1; i
的PHP
输出:
skeegrofskeeg
时间复杂度: O(n)
辅助空间: O(1)
方法4(递归)
C++
// Recursive C++ program to reverse a string
#include
using namespace std;
void recursiveReverse(string &str, int i = 0)
{
int n = str.length();
if (i == n / 2)
return;
swap(str[i], str[n - i - 1]);
recursiveReverse(str, i + 1);
}
// Driver program
int main()
{
string str = "geeksforgeeks";
recursiveReverse(str);
cout << str;
return 0;
}
Java
// Recursive Java program to reverse a string
class GFG
{
static void recursiveReverse(char[] str, int i)
{
int n = str.length;
if (i == n / 2)
return;
swap(str,i,n - i - 1);
recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
char temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
// Driver program
public static void main(String[] args)
{
char[] str = "geeksforgeeks".toCharArray();
recursiveReverse(str,0);
System.out.println(String.valueOf(str));
}
}
// This code is contributed by 29AjayKumar
Python3
# Recursive Python program to reverse a string
def recursiveReverse(str, i = 0):
n = len(str)
if i == n // 2:
return
str[i], str[n-i-1] = str[n-i-1], str[i]
recursiveReverse(str, i+1)
if __name__ == "__main__":
str = "geeksforgeeks"
# converting string to list
# because strings do not support
# item assignment
str = list(str)
recursiveReverse(str)
# converting list to string
str = ''.join(str)
print(str)
# This code is contributed by
# sanjeev2552
C#
// Recursive C# program to reverse a string
using System;
public class GFG
{
static void recursiveReverse(char[] str, int i)
{
int n = str.Length;
if (i == n / 2)
return;
swap(str,i,n - i - 1);
recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
char temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
// Driver program
public static void Main(String[] args)
{
char[] str = "geeksforgeeks".ToCharArray();
recursiveReverse(str,0);
Console.WriteLine(String.Join("",str));
}
}
// This code is contributed by Princi Singh
C++
// A quickly written program for reversing a string
// using reverse()
#include
using namespace std;
int main()
{
string str = "geeksforgeeks";
// Reverse str[beign..end]
reverse(str.begin(),str.end());
cout << str;
return 0;
}
Java
// A Simple Java program
// to reverse a string
class GFG
{
public static void main(String[] args)
{
String str = "geeksforgeeks";
// Reverse str[beign..end]
str = reverse(str);
System.out.println(str);
}
static String reverse(String input)
{
char[] temparray = input.toCharArray();
int left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
}
// This code is contributed by 29AjayKumar
Python
# A Simple python program
# to reverse a string
# Function to
# reverse a string
def reverseStr(str):
# print the string
# from last
print(str[::-1])
# Driver Code
def main():
str = "geeksforgeeks";
reverseStr(str);
if __name__=="__main__":
main()
# This code is contributed
# by prabhat kumar singh
C#
// A Simple C# program to reverse a string
using System;
class GFG
{
public static void Main(String[] args)
{
String str = "geeksforgeeks";
// Reverse str[beign..end]
str = reverse(str);
Console.WriteLine(str);
}
static String reverse(String input)
{
char[] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join("",temparray);
}
}
/* This code is contributed by PrinciRaj1992 */
的PHP
输出:
skeegrofskeeg