BAEL-1525: More changes.

This commit is contained in:
shouvikbhattacharya 2018-02-07 20:29:35 +05:30
parent 604f5b1535
commit 6887bc10d1
2 changed files with 63 additions and 1 deletions

View File

@ -8,7 +8,33 @@ public class Palindrome {
int forward = 0;
int backward = length - 1;
boolean palindrome = true;
while ((backward > forward)?(palindrome=(text.charAt(forward++) == text.charAt(backward--))):false);
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return palindrome;
}
public boolean isPalindromeReverseTheString(String text) {
String reverse = "";
text = text.toLowerCase();
char[] plain = text.toCharArray();
for (int i = plain.length - 1; i >= 0; i--)
reverse += plain[i];
return reverse.equals(text);
}
public boolean isPalindromeUsingStringBuilder(String text) {
StringBuilder plain = new StringBuilder(text);
StringBuilder reverse = plain.reverse();
return reverse.equals(plain);
}
public boolean isPalindromeUsingStringBuffer(String text) {
StringBuffer plain = new StringBuffer(text);
StringBuffer reverse = plain.reverse();
return reverse.equals(plain);
}
}

View File

@ -34,4 +34,40 @@ public class WhenCheckingPalindrome {
for(String sentence:sentences)
Assert.assertTrue(palindrome.isPalindrome(sentence));
}
@Test
public void whenReverseWord_shouldBePalindrome() {
for(String word:words)
Assert.assertTrue(palindrome.isPalindromeReverseTheString(word));
}
@Test
public void whenReverseSentence_shouldBePalindrome() {
for(String sentence:sentences)
Assert.assertTrue(palindrome.isPalindromeReverseTheString(sentence));
}
@Test
public void whenStringBuilderWord_shouldBePalindrome() {
for(String word:words)
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(word));
}
@Test
public void whenStringBuilderSentence_shouldBePalindrome() {
for(String sentence:sentences)
Assert.assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence));
}
@Test
public void whenStringBufferWord_shouldBePalindrome() {
for(String word:words)
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(word));
}
@Test
public void whenStringBufferSentence_shouldBePalindrome() {
for(String sentence:sentences)
Assert.assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence));
}
}