-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeChecker.java
50 lines (40 loc) · 1.26 KB
/
PalindromeChecker.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
43
44
45
46
47
48
49
50
import java.util.Scanner;
class PalindromeChecker {
// Main method
public static void main(String[] args) {
// Method to take string input from user
String str = getPalindrome();
// Method to check if a given string is palindrome
boolean result = isPalindrome(str);
// Method to display the result
printResult(str, result);
}
// method to take string input from user
public static String getPalindrome() {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
return str;
}
// Method to check if a given string is palindrome
public static boolean isPalindrome(String str) {
int start = 0;
int end = str.length() - 1;
while(start < end) {
if(str.charAt(start) != str.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
// Method to display the result
public static void printResult(String s, boolean result) {
if(result) {
System.out.println(s + " is a palindrome");
} else {
System.out.println(s + " is not a palindrome");
}
}
}