Recursive way to determine whether a string is a palindrome:
Palindrome is the correct reading, the opposite of reading all the same sentence. For example, "I am who I was."
We used the charAt () method, for example, the following example: charAt () method returns the character at the specified index. The index range is from 0 to length () - 1.
grammar:
public char charAt(int index)
parameter
-
index - index of the character.
return value
Returns the character at the specified index.
public class Test { public static void main(String args[]) {
String s = "www.runoob.com";
char result = s.charAt(8);
System.out.println(result);
}
}
String s = "www.runoob.com";
char result = s.charAt(8);
System.out.println(result);
}
}
The above procedure execution results:
The
Determining whether the palindrome, first, the user inputs a string, and then to determine the length of the string, or if a zero length, it certainly is palindromic, if the length is greater than or equal to 2, then first determines the first character to the last character, is equal to the interception of the second to penultimate, then determining, using a recursive method.
// source code
package lianxi1;
import java.util.*;
public class Palindrome {
import java.util.*;
public class Palindrome {
static public boolean isPa(String f,int n){
if(f.charAt(0)==f.charAt(f.length()-1)) {
if(f.length()>2) {
return isPa(f.substring(n+1,f.length()-1),0); //从n+1到(f.length()-1)-1
}
else {
return true;
}
}
else return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
if(f.charAt(0)==f.charAt(f.length()-1)) {
if(f.length()>2) {
return isPa(f.substring(n+1,f.length()-1),0); //从n+1到(f.length()-1)-1
}
else {
return true;
}
}
else return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scan = new new Scanner Scanner (the System.in);
System.out.println ( "Please enter the string:");
String scan.next F = ();
IF (ispA (F, 0)) {
System.out.println ( "string:" + f + "palindromic sequence");
}
the else {
System.out.println ( "string:" + f + "is not palindromic sequence");
}
}
System.out.println ( "Please enter the string:");
String scan.next F = ();
IF (ispA (F, 0)) {
System.out.println ( "string:" + f + "palindromic sequence");
}
the else {
System.out.println ( "string:" + f + "is not palindromic sequence");
}
}
}