-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample13.java
58 lines (42 loc) · 1.38 KB
/
Example13.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
package applications.algorithms;
import java.util.ArrayList;
/** Category: Algorithms
* ID: Example13
* Description: Test if a given string is a palindrome
* Taken From:
* Details:
* TODO
*/
public class Example13 {
public static boolean isPalindrome(String s){
if(s == null ){
throw new NullPointerException("Null string given");
}
char[] data = s.toLowerCase().toCharArray();
// invert the string
int start = 0;
int end = s.length()-1;
int middle = s.length() >> 1;
for (int i = 0; i < middle; ++i) {
char temp = s.charAt(i);
data[i] = data[end];
data[end--] = temp;
}
String other = new String(data);
return other.equals(s);
}
public static void main(String[] args){
String s = "eve";
boolean palindrome = isPalindrome(s);
System.out.println("String " +s + (palindrome ? " is" : " is not") +" a palindrome" );
System.out.println("\n");
s = "Alex";
palindrome = isPalindrome(s);
System.out.println("String " +s + (palindrome ? " is" : " is not") +" a palindrome" );
System.out.println("\n");
s = "A";
palindrome = isPalindrome(s);
System.out.println("String " +s + (palindrome ? " is" : " is not") +" a palindrome" );
System.out.println("\n");
}
}