BAEL-1525: Article code completed.
This commit is contained in:
parent
ff76cbc1fe
commit
581e8592ae
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.string;
|
||||||
|
|
||||||
|
public class Palindrome {
|
||||||
|
|
||||||
|
public boolean isPalindrome(String text) {
|
||||||
|
text = text.replaceAll("\\s+", "").toLowerCase();
|
||||||
|
int length = text.length();
|
||||||
|
int forward = 0;
|
||||||
|
int backward = length - 1;
|
||||||
|
boolean palindrome = true;
|
||||||
|
while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false);
|
||||||
|
return palindrome;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.baeldung.string;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class WhenCheckingPalindrome {
|
||||||
|
|
||||||
|
private String[] words = {
|
||||||
|
"Anna",
|
||||||
|
"Civic",
|
||||||
|
"Kayak",
|
||||||
|
"Level",
|
||||||
|
"Madam",
|
||||||
|
};
|
||||||
|
|
||||||
|
private String[] sentences = {
|
||||||
|
"Sore was I ere I saw Eros",
|
||||||
|
"Euston saw I was not Sue",
|
||||||
|
"Too hot to hoot",
|
||||||
|
"No mists or frost Simon",
|
||||||
|
"Stella won no wallets"
|
||||||
|
};
|
||||||
|
|
||||||
|
private Palindrome palindrome = new Palindrome();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenWord_shouldBePalindrome() {
|
||||||
|
for(String word:words)
|
||||||
|
Assert.assertTrue(palindrome.isPalindrome(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSentence_shouldBePalindrome() {
|
||||||
|
for(String sentence:sentences)
|
||||||
|
Assert.assertTrue(palindrome.isPalindrome(sentence));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue