CheckPalindrome
来源:互联网 发布:结婚算日子软件 编辑:程序博客网 时间:2024/06/07 02:43
import java.util.Scanner;
public class CheckPalindrome {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string
System.out.println("Enter a string");
String string = input.next();
System.out.println(string + " is a palindrome? " +
isPalindrome(string));
}
static boolean isPalindrome(String string) {
char[] ch = string.toCharArray();
for(int i = 0; i < ch.length / 2; i++)
if(ch[i] != ch[ch.length - 1 - i])
return false;
return true;
}
/** Check if a string is palindrome */
public static boolean isPalindrome_1(String s) {
// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = s.length() - 1;
while(low < high) {
if(s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
}
Enter a string
noon
noon is a palindrome? true
public class CheckPalindrome {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string
System.out.println("Enter a string");
String string = input.next();
System.out.println(string + " is a palindrome? " +
isPalindrome(string));
}
static boolean isPalindrome(String string) {
char[] ch = string.toCharArray();
for(int i = 0; i < ch.length / 2; i++)
if(ch[i] != ch[ch.length - 1 - i])
return false;
return true;
}
/** Check if a string is palindrome */
public static boolean isPalindrome_1(String s) {
// The index of the first character in the string
int low = 0;
// The index of the last character in the string
int high = s.length() - 1;
while(low < high) {
if(s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
}
Enter a string
noon
noon is a palindrome? true
0 0