Skip to content

Commit cfc601c

Browse files
committed
Palindrome
1 parent ecb4fa9 commit cfc601c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/easy/Palindrome.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package easy;
2+
3+
/**
4+
* Have the function Palindrome(str) take the str parameter
5+
* being passed and return the string true
6+
* if the parameter is a palindrome,
7+
* (the string is the same forward as it is backward)
8+
* otherwise return the string false.
9+
* For example: "racecar" is also "racecar" backwards.
10+
* Punctuation and numbers will not be part of the string.
11+
*/
12+
public class Palindrome {
13+
14+
/**
15+
* Palindrome function.
16+
*
17+
* @param str input string
18+
* @return "true" is the string is a palindrome
19+
*/
20+
private static String palindrome(String str) {
21+
StringBuilder reversed = new StringBuilder();
22+
String cleaned = str.replaceAll(" ", "");
23+
reversed.append(cleaned).reverse();
24+
return cleaned.equals(reversed.toString()) && str.length() > 0 ? "true" : "false";
25+
}
26+
27+
/**
28+
* Entry point.
29+
*
30+
* @param args command line arguments
31+
*/
32+
public static void main(String[] args) {
33+
var result1 = palindrome("dont nod");
34+
System.out.println(result1);
35+
var result2 = palindrome("rats live on no evil star");
36+
System.out.println(result2);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy