Skip to content

Commit 581e859

Browse files
BAEL-1525: Article code completed.
1 parent ff76cbc commit 581e859

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.string;
2+
3+
public class Palindrome {
4+
5+
public boolean isPalindrome(String text) {
6+
text = text.replaceAll("\\s+", "").toLowerCase();
7+
int length = text.length();
8+
int forward = 0;
9+
int backward = length - 1;
10+
boolean palindrome = true;
11+
while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false);
12+
return palindrome;
13+
}
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.string;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class WhenCheckingPalindrome {
7+
8+
private String[] words = {
9+
"Anna",
10+
"Civic",
11+
"Kayak",
12+
"Level",
13+
"Madam",
14+
};
15+
16+
private String[] sentences = {
17+
"Sore was I ere I saw Eros",
18+
"Euston saw I was not Sue",
19+
"Too hot to hoot",
20+
"No mists or frost Simon",
21+
"Stella won no wallets"
22+
};
23+
24+
private Palindrome palindrome = new Palindrome();
25+
26+
@Test
27+
public void whenWord_shouldBePalindrome() {
28+
for(String word:words)
29+
Assert.assertTrue(palindrome.isPalindrome(word));
30+
}
31+
32+
@Test
33+
public void whenSentence_shouldBePalindrome() {
34+
for(String sentence:sentences)
35+
Assert.assertTrue(palindrome.isPalindrome(sentence));
36+
}
37+
}

0 commit comments

Comments
 (0)