-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPalindrome.java
More file actions
44 lines (34 loc) · 1.1 KB
/
Palindrome.java
File metadata and controls
44 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package loop;
import java.util.Scanner;
/**
* 如果一个字符串从前往后,以及从后往前是一样的,那么它就是一个回文。例如, “ mom”、“ dad ”,以及“ noon”, 都是回文。
* 要解决的问题是,编写一个程序,提示用户输人一个字符串,然后给出该字符串是否是 回文。
*/
public class Palindrome {
public static void main(String[] args) {
// Math.random();
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String s = input.nextLine();
// 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;
boolean isPalindrome = true;
while (low < high) {
if (s.charAt(low) != s.charAt(high)) {
isPalindrome = false;
break;
}
low++;
high--;
}
if (isPalindrome) {
System.out.println(s + " is a palindrome");
} else {
System.out.println(s + " is not a palindrome");
}
}
}