Java 程序:使用递归检查回文字符串

原文:https://www.studytonight.com/java-programs/java-program-to-check-palindrome-string-using-recursion

在本教程中,我们将学习如何使用递归函数检查字符串是否是回文。递归函数是调用自身的函数。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。

输入:输入字符串:妈妈

输出:输入的字符串是回文。

让我们看看这个程序,看看这个字符串是否是回文。

程序 1:使用递归检查回文字符串

在这个程序中,我们将学习如何使用递归检查一个字符串是否是回文。在这里,我们将要求用户输入字符串。然后,我们将调用一个单独的递归函数来检查该字符串是否是回文,如果输入的字符串不是空的。如果字符串是空的,那么它将打印出它是一个回文。

算法

  1. 开始
  2. 声明一个字符串变量。
  3. 要求用户初始化字符串。
  4. 调用函数检查字符串是否回文。
  5. 如果一个字符串是空的,那么它就是一个回文。
  6. 如果字符串不为空,则调用递归函数。
  7. 如果只有一个字符,那么它就是回文。
  8. 如果第一个和最后一个字符不匹配,那么它就不是回文。
  9. 如果有多个字符,请检查中间的子字符串是否也是回文或者没有使用递归函数。
  10. 打印结果。
  11. 停下来。

下面是相同的 Java 语言代码。

/*Java Program to Check whether a String is a Palindrome or not using Recursive Function*/
import java.util.Scanner;
public class Main
{
    //Recursive function that checks 
    //whether the string is palindrome or not
    static boolean checkPalindrome(String str, int s, int e) 
    { 
        if (s == e)    // If there is only one character 
            return true;  
        // If first and last characters do not match 
        if ((str.charAt(s)) != (str.charAt(e))) 
            return false;   
        // If there are multiple characters, check if 
        // middle substring is also palindrome or not. 
        if (s < e + 1) 
            return checkPalindrome(str, s + 1, e - 1);   
        return true; 
    }   
    static boolean isPalindrome(String str) 
    { 
        int n = str.length();   
    // If string is empty,then it is palindrome 
        if (n == 0) 
            return true;   
        return checkPalindrome(str, 0, n - 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
        //Check whether palindrome or not
        if (isPalindrome(str)) 
            System.out.println(str+" is palindrome"); 
        else
            System.out.println(str+ " is not a palindrome"); 
    }   
}

输入字符串:哇 哇是回文

程序 2:使用递归检查回文字符串

在这个程序中,我们将学习如何使用递归检查一个字符串是否是回文。这里,一旦用户输入了字符串,我们将调用一个递归函数,通过比较子字符串的第一个和最后一个字符来检查它是否是回文。

算法

  1. 开始
  2. 声明一个字符串变量。
  3. 要求用户初始化字符串。
  4. 调用递归函数检查字符串是否回文。
  5. 如果一个字符串是空的或者只有一个字符,那么它就是回文。
  6. 如果有多个字符,则检查字符串的第一个和最后一个字符。
  7. 如果字符串的第一个和最后一个字符相同,则对子字符串执行相同的操作,去掉第一个和最后一个字符。
  8. 继续该过程,直到条件失败。
  9. 显示结果。
  10. 停下来。

下面是相同的 Java 语言代码。

/*Java Program to Check whether a String is a Palindrome or not using Recursive Function*/
import java.util.Scanner;
public class Main
{
    //Recursive function that checks 
    //whether the string is palindrome or not
    static boolean isPalindrome(String str) 
    { 
        //If string has 0 or 1 character
        if(str.length() == 0 || str.length() == 1)
            return true; 
        //If string has multiple characters
        //Check whether first and last characters are same or not
        if(str.charAt(0) == str.charAt(str.length()-1))
            return isPalindrome(str.substring(1, str.length()-1));
        return false;
    }   
    // 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
        //Check whether palindrome or not
        if (isPalindrome(str)) 
            System.out.println(str+" is palindrome"); 
        else
            System.out.println(str+ " is not a palindrome"); 
    }     
}

输入字符串:hello hello 不是回文