Java 程序:使用递归反转字符串
原文:https://www.studytonight.com/java-programs/java-program-to-reverse-a-string-using-recursion
在本教程中,我们将学习如何使用递归函数反转字符串。递归函数是调用自身的函数。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。
输入:输入字符串:字符串
输出:字符串的反转为:gnirtS
程序 1:使用递归反转字符串
在这个程序中,我们将看到如何使用带有用户定义字符串的递归来反转字符串。在这里,我们将要求用户输入字符串,然后我们将通过递归调用函数来反转该字符串,最后将打印反转的字符串。
算法
- 开始
- 声明一个字符串。
- 要求用户初始化字符串。
- 调用递归函数来反转字符串。
- 如果字符串为空或由单个字符组成,则打印输入的字符串。
- 如果字符串由多个字符组成,则递归调用函数来反转字符串。
- 打印反转的字符串。
- 停下来。
下面是相同的 Java 语言代码。
/*Java Program to reverse a string using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Recursive function that reverses a string
static void reverse(String str)
{
//If the string is null or consists of single character
//then print the entered string
if ((str==null)||(str.length() <= 1))
System.out.println(str);
else
{
//If string consists of multiple strings
System.out.print(str.charAt(str.length()-1));
//Call the function recursively to reverse the string
reverse(str.substring(0,str.length()-1));
}
}
// Driver Code
public static void main(String args[])
{
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.nextLine(); //Input the string
//Call a recursive function to reverse the string
System.out.println("The reverse of the entered the String :");
reverse(str);
}
}
进入字符串: 你好世界 进入字符串的反义词: dlrow olleh
程序 2:使用递归反转字符串
在这个程序中,我们将看到如何使用带有用户定义字符串的递归来反转字符串。在这里,我们将要求用户输入字符串,然后我们将通过递归调用函数来反转该字符串,并将返回反转后的字符串。最后,显示反转的字符串。
算法
- 开始
- 声明一个字符串。
- 初始化它。
- 调用递归函数来反转字符串。
- 如果字符串为空,即如果字符串为空,则返回相同的字符串。
- 如果字符串有多个字符,则递归调用函数来反转字符串。
- 返回反转的字符串。
- 打印反转的字符串。
- 停下来。
下面是相同的 Java 语言代码。
/*Java Program to reverse a string using Recursive Function*/
import java.util.Scanner;
public class Main
{
//Driver Code
public static void main(String[] args)
{
//Take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String :");
String str = sc.nextLine(); //Input the string
//Call a recursive function to reverse the string
String rev=reverseString(str);
System.out.println("The reverse of the entered the String :"+rev);
}
//Recursive Function to Reverse the String
public static String reverseString(String str)
{
//If entered string is empty
//Return the empty string
if (str.isEmpty())
return str;
//If string consists of multiple character
//Call the Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}
输入字符串:世界 输入字符串的反方:dlroW
程序 3:使用递归反转字符串
在这个程序中,我们将看到如何使用带有预定义字符串的递归来反转字符串。
算法
- 开始
- 声明一个字符串。
- 初始化它。
- 调用递归函数来反转字符串。
- 如果字符串为空,即如果字符串为空,则返回相同的字符串。
- 如果字符串有多个字符,则递归调用函数来反转字符串。
- 打印反转的字符串。
- 停下来。
下面是相同的 Java 语言代码。
/*Java Program to reverse a string using Recursive Function*/
public class Main
{
//Driver Code
public static void main(String[] args)
{
//Initialize the String
String str = "Reverse String";
System.out.println("The entered string is: " + str);
String rev = reverseString(str);
System.out.println("The reversed string is: " + rev);
}
//Recursive Function to Reverse the String
public static String reverseString(String str)
{
//If entered string is empty
//Return the empty string
if (str.isEmpty())
return str;
//If string consists of multiple character
//Call the Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}
输入的字符串是:反向字符串 反向字符串是:gnirtS esreveR