Skip to content

Commit da5c1dc

Browse files
authored
Incorporating review comments
1 parent c649b68 commit da5c1dc

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

java/PalindromeCheck.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ public class PalindromeCheck {
88
public static void main(String[] args) {
99
// Accept input from the console
1010
System.out.print("Please enter a string: ");
11+
String input = null;
1112
String s = null;
1213
try {
1314
Scanner sc = new Scanner(System.in);
14-
s = sc.next();
15+
input = sc.next();
16+
s = input.toLowerCase(); // convert all characters to lower case so that palindrome check is case insensitive.
1517
sc.close();
1618
} catch (Exception e) {
1719
outputAndEnd("Unexpected error, please try to re-run the program."); // Error message for any unknown issues while reading the input
1820
}
1921

20-
char[] chars = s.toCharArray();
21-
int len = chars.length;
22+
int len = s.length();
2223

2324
/*
2425
Loop through all the characters and evaluate if given string is a palindrome
@@ -29,12 +30,12 @@ public static void main(String[] args) {
2930
m(f) and a(r) from beginning are compared against m(t) and a(i) from the end, the middle character d(u) is ignored
3031
*/
3132
for (int i = 0; i < len / 2; i++) {
32-
if (chars[i] != chars[len - 1 - i]) {
33-
outputAndEnd(s + " is not a palindrome"); // Report failure when the first mismatch is found.
33+
if (s.charAt(i) != s.charAt(len - 1 - i)) {
34+
outputAndEnd(input + " is not a palindrome"); // Report failure when the first mismatch is found.
3435
}
3536
}
3637
// If no mismatch is found and the loop is successfully complete, report the success.
37-
outputAndEnd(s + " is a palindrome");
38+
outputAndEnd(input + " is a palindrome");
3839
}
3940

4041
private static void outputAndEnd(String message) {

0 commit comments

Comments
 (0)