9.23 Classroom Summary

Today’s manual brain is a program to judge whether it is a palindrome;

First, we must create a string input from the keyboard, and then use a do The while loop, and then define an integer variable to store the length of the input string.

Then I wrote a test function, used to recursively determine whether the input string is a palindrome, defined two character variables to intercept the characters corresponding to the comparison, and then used two If judge the sentence to judge

judge. Recursion is used in the second if statement. When recursing, the length of the string is subtracted by 1.

Main program:

package TWO;

import java.util.Scanner;

public class Palindrome {
public static void main(String[] args) {
int k=1;
do{
System.out.println(“Please enter a string”);
Scanner sc=new Scanner( System.in);
String str=sc.nextLine();
int size=str.length();
int m=test(str,size);
if(m== 1)
System.out.println(“This string is a palindrome”);
else {
System.out.println(“This string is not a palindrome”);
}< br> }while(k==1);
}

public static int test(String str,int size){
int a,b,j=0;
char c1 ,c2;
a=str.length()-size;
b=str.length()-(a+1);
//Get string
c1=str.charAt( a);
c2=str.charAt(b);
//Judgment when the input is one or zero string;
if(a==b||c1==c2){
j=1;
}
if(a!=b&&a test(str, size-1);
return j;
}
}

Summary:

I think the most difficult part when designing this program is thinking about designing ideas when judging whether it is a palindrome; how to achieve it, How many variables are defined, I think it’s not programming but thinking about how to program.

Leave a Comment

Your email address will not be published.