Use recursive method to determine whether a string is a palindrome (palindrome)

Source: Internet
Author: User

"Palindrome" is the same sentence that reads and reads the opposite. Like, "Who am I?" the algorithm for detecting palindrome using recursive algorithm is described as follows: a single or Zero-character string is a palindrome. Any other string is a palindrome if the first and last characters is the same, and the string that remains, excepting tho Se characters, is a palindrome.

Palindrome code is as follows:

Package Huiwen;
Import Java.util.Scanner;
public class Huiwen {
public static Boolean ispalindrome (String s,int i,int j) {
if (i > J)
throw new IllegalArgumentException ();
if (i = = j)
return true;
else{
Return (S.charat (i) = = S.charat (j)) && Ispalindrome (s,i+1,j-1);
}
}

public static void Main (string[] args) {
Scanner in=new Scanner (system.in);
String s = in.nextline ();
int i = 0;
Int J = s.length ()-1;
SYSTEM.OUT.PRINTLN (S + "is palindrome?" + Huiwen. Ispalindrome (S, I, j));
}
}

Code:

Use recursive method to determine whether a string is a palindrome (palindrome)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.