forked from ShivangiSingh17/Java-Jet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.java
More file actions
41 lines (36 loc) · 1.34 KB
/
PalindromeChecker.java
File metadata and controls
41 lines (36 loc) · 1.34 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
public class PalindromeChecker {
//Check wether a string is palindrome, return true or false
public boolean isPalindrome(String s){
int length = s.length();
for (int i = 0; i < length; i++){
if (s.charAt(i) != s.charAt(length-1))
return false;
length--;
}
return true;
}
//Check wether a string is palindrome by using recursive method, return true or false
public boolean isPalindromeRecursive(String s){
int length = s.length();
//Termination
if (length < 2)
return true;
else {
if (s.charAt(0) == s.charAt(length-1))
this.isPalindromeRecursive(s.substring(1, length-1))
else
return false;
}
}
public static void main(String[] args) {
PalindromeChecker pc = new PalindromeChecker();
System.out.println("Palindrome Test");
System.out.println(pc.isPalindrome("test"));
System.out.println(pc.isPalindrome("bob"));
System.out.println(pc.isPalindrome("kasurrusak"));
System.out.println("Palindrome Recursive Test");
System.out.println(pc.isPalindrome("test"));
System.out.println(pc.isPalindrome("bob"));
System.out.println(pc.isPalindrome("kasurrusak"));
}
}