-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPalindrome.java
42 lines (33 loc) · 986 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
41
42
package chapter_five.samples;
import java.util.Scanner;
/** Listing 5.14 Palindrome.java */
public class Palindrome
{
/** 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.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");
}
}