forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome3.java
More file actions
34 lines (23 loc) · 753 Bytes
/
Palindrome3.java
File metadata and controls
34 lines (23 loc) · 753 Bytes
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
package com.zetcode;
// A palindrome is a word, number, phrase, or other sequence of characters
// which reads the same backward as forward, such as madam or racecar
public class Palindrome3 {
public static void main(String[] args) {
System.out.println(isPalindrome("radar"));
System.out.println(isPalindrome("kayak"));
System.out.println(isPalindrome("forest"));
}
private static boolean isPalindrome(String original) {
char[] data = original.toCharArray();
int i = 0;
int j = data.length - 1;
while (j > i) {
if (data[i] != data[j]) {
return false;
}
++i;
--j;
}
return true;
}
}