Creating a recursive method for Palindrome

2019-01-11 11:45发布

I am trying to create a Palindrome program using recursion within Java but I am stuck, this is what I have so far:

 public static void main (String[] args){
 System.out.println(isPalindrome("noon"));
 System.out.println(isPalindrome("Madam I'm Adam"));
 System.out.println(isPalindrome("A man, a plan, a canal, Panama"));
 System.out.println(isPalindrome("A Toyota"));
 System.out.println(isPalindrome("Not a Palindrome"));
 System.out.println(isPalindrome("asdfghfdsa"));
}

public static boolean isPalindrome(String in){
 if(in.equals(" ") || in.length() == 1 ) return true;
 in= in.toUpperCase();
 if(Character.isLetter(in.charAt(0))
}

public static boolean isPalindromeHelper(String in){
 if(in.equals("") || in.length()==1){
  return true;
  }
 }
}

Can anyone supply a solution to my problem?

21条回答
霸刀☆藐视天下
2楼-- · 2019-01-11 12:19

Here is the code for palindrome check without creating many strings

public static boolean isPalindrome(String str){
    return isPalindrome(str,0,str.length()-1);
}

public static boolean isPalindrome(String str, int start, int end){
    if(start >= end)
        return true;
    else
        return (str.charAt(start) == str.charAt(end)) && isPalindrome(str, start+1, end-1);
}
查看更多
孤傲高冷的网名
3楼-- · 2019-01-11 12:20

for you to achieve that, you not only need to know how recursion works but you also need to understand the String method. here is a sample code that I used to achieve it: -

class PalindromeRecursive {
  public static void main(String[] args) {


    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a string");
    String input=sc.next();
    System.out.println("is "+ input + "a palindrome : " +  isPalindrome(input));


  }

  public static  boolean isPalindrome(String s)
  {
    int low=0;
    int high=s.length()-1;
    while(low<high)
    {
      if(s.charAt(low)!=s.charAt(high))
      return false;
      isPalindrome(s.substring(low++,high--));
    }

    return true;
  }
}
查看更多
男人必须洒脱
4楼-- · 2019-01-11 12:22
/**
     * Function to check a String is palindrome or not
     * @param s input String
     * @return true if Palindrome
     */
    public boolean checkPalindrome(String s) {

        if (s.length() == 1 || s.isEmpty())
            return true;

        boolean palindrome = checkPalindrome(s.substring(1, s.length() - 1));

        return palindrome && s.charAt(0) == s.charAt(s.length() - 1);

    }
查看更多
时光不老,我们不散
5楼-- · 2019-01-11 12:23

Palindrome example:

static boolean isPalindrome(String sentence) {

    /*If the length of the string is 0 or 1(no more string to check), 
     *return true, as the base case. Then compare to see if the first 
     *and last letters are equal, by cutting off the first and last 
     *letters each time the function is recursively called.*/

    int length = sentence.length();

    if (length >= 1)
        return true;
    else {
        char first = Character.toLowerCase(sentence.charAt(0));
        char last = Character.toLowerCase(sentence.charAt(length-1));

        if (Character.isLetter(first) && Character.isLetter(last)) {
            if (first == last) {
                String shorter = sentence.substring(1, length-1);
                return isPalindrome(shorter);
            } else {
                return false;
            }
        } else if (!Character.isLetter(last)) {
            String shorter = sentence.substring(0, length-1);
            return isPalindrome(shorter);
        } else {
            String shorter = sentence.substring(1);
            return isPalindrome(shorter);
        }
    }
}

Called by:

System.out.println(r.isPalindrome("Madam, I'm Adam"));

Will print true if palindrome, will print false if not.

If the length of the string is 0 or 1(no more string to check), return true, as the base case. This base case will be referred to by function call right before this. Then compare to see if the first and last letters are equal, by cutting off the first and last letters each time the function is recursively called.

查看更多
聊天终结者
6楼-- · 2019-01-11 12:25
public class palin
{ 
    static boolean isPalin(String s, int i, int j)
    {
        boolean b=true;
        if(s.charAt(i)==s.charAt(j))
        {
            if(i<=j)
                isPalin(s,(i+1),(j-1));
        }
        else
        {
            b=false;
        }
        return b;
    }
    public static void main()
    {
        String s1="madam";
        if(isPalin(s1, 0, s1.length()-1)==true)
            System.out.println(s1+" is palindrome");
        else
            System.out.println(s1+" is not palindrome");
    }
}
查看更多
小情绪 Triste *
7楼-- · 2019-01-11 12:25
public class chkPalindrome{

public static String isPalindrome(String pal){

if(pal.length() == 1){

return pal;
}
else{

String tmp= "";

tmp = tmp + pal.charAt(pal.length()-1)+isPalindrome(pal.substring(0,pal.length()-1));

return tmp;
}


}
     public static void main(String []args){

         chkPalindrome hwObj = new chkPalindrome();
         String palind = "MADAM";

       String retVal= hwObj.isPalindrome(palind);
      if(retVal.equals(palind))
       System.out.println(palind+" is Palindrome");
       else
       System.out.println(palind+" is Not Palindrome");
     }
}
查看更多
登录 后发表回答