File tree Expand file tree Collapse file tree
main/java/com/baeldung/string
test/java/com/baeldung/string Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments