-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindrome.java
73 lines (54 loc) · 2.37 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tomkous.algos;
import java.util.Scanner;
public class Palindrome {
public static boolean isPalindrome(String word) throws UnsupportedOperationException {
for (int i = 1; i<=(word.length()+1)/2; i++){
System.out.println("i is " + i);
System.out.println(String.valueOf(word.charAt(i-1)));
System.out.println(String.valueOf(word.charAt(word.length()-i)));
if (String.valueOf(word.charAt(i-1)).equalsIgnoreCase(String.valueOf(word.charAt(word.length()-i))))
continue;
else
return false;
}
return true;
}
public static boolean isPalindrome1(String word) throws UnsupportedOperationException {
System.out.println(word.substring(0,word.length()));
System.out.println("comparing " + word.substring(0,1) + " " + word.substring(word.length()-1,word.length()));
if (word.length()==1){
System.out.println("word length is " + word.length());
System.out.println("returning true ");
return true;
} else if(word.substring(0,1).equalsIgnoreCase(word.substring(word.length()-1,word.length()))){
if (word.length()==2){
return true;
}
else {
System.out.println("word length is " + word.length());
return isPalindrome1(word.substring(1, word.length()-1));
}
}
return false;
}
public static void isPalindrome2()
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equalsIgnoreCase(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string isn't a palindrome.");
}
public static void main(String[] args) {
System.out.println(Palindrome.isPalindrome("Racecar"));
System.out.println(Palindrome.isPalindrome1("Avocado"));
System.out.println(Palindrome.isPalindrome1("Eve"));
Palindrome.isPalindrome2();
}
}