With JAVA determine whether the passage palindrome

Experimental requirements: recursively determines whether a string is a palindrome. (Palindrome is a positive, forward and read the opposite of reading all the same sentence as "Who am I that I").

 

Design ideas: first ask the user to enter a word, then select the first character with the last character input function dg, dg function will first determine whether this statement is empty from this statement, if the output is empty directly measured palindrome . Otherwise, if the two are equal on the first character input Analyzing these two character positions in the string is equal to or a difference. If the output is a palindrome, and vice versa continue to call the function dg, recursively. After entering the first character is a character bit by bit and before the last character. And if these two characters are not equal can be directly output is not a palindrome.

Source Code:

 

import java.util.Scanner;


public class Text {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("输入一句话");
String a=sc.next();
int b=a.length();
dg(a,0,b-1);
}

public static void dg(String a,int i, int j)
{
if(a.length()==0)
System.out.println("是回文! ");
if(a.charAt(i)==a.charAt(j))
{


if(i==j||j-i==1)
{
System.out.println("是回文! ");
}
else
dg(a,++i,--j);
}
else
System.out.println("不是回文!");
}

}

Summary analysis program: This program uses a recursive method, let the program more concise and more readable.

PSP0 level:

date Starting time End Time total time project Remark
2018/9/23 3:30 3:45 15min Determine whether the passage palindrome To use the recursive method

Guess you like

Origin www.cnblogs.com/sicilya/p/11585154.html