Skip to content

Commit 9fb91cd

Browse files
authored
Create Palindrome3.java
1 parent f79a8d3 commit 9fb91cd

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

algorithms/Palindrome3.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.zetcode;
2+
3+
// A palindrome is a word, number, phrase, or other sequence of characters
4+
// which reads the same backward as forward, such as madam or racecar
5+
6+
public class Palindrome3 {
7+
8+
public static void main(String[] args) {
9+
10+
System.out.println(isPalindrome("radar"));
11+
System.out.println(isPalindrome("kayak"));
12+
System.out.println(isPalindrome("forest"));
13+
}
14+
15+
private static boolean isPalindrome(String original) {
16+
17+
char[] data = original.toCharArray();
18+
19+
int i = 0;
20+
int j = data.length - 1;
21+
22+
while (j > i) {
23+
24+
if (data[i] != data[j]) {
25+
return false;
26+
}
27+
28+
++i;
29+
--j;
30+
}
31+
32+
return true;
33+
}
34+
}

0 commit comments

Comments
 (0)