-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindrome.java
40 lines (30 loc) · 969 Bytes
/
Palindrome.java
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
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create a scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string:
System.out.println("Please enter a string");
String s = input.nextLine();
// The index of the first character:
int low = 0;
// The index of the last character:
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");
}
}
}