-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursivePalindrome.java
46 lines (42 loc) · 1 KB
/
RecursivePalindrome.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* This program contains the method to determine whether a string is a palindrome.
*
* @author Justin Diep
* @version 4-19-18
*/
public class RecursivePalindrome
{
//This method helps deal with upper case, lower case, punctuation and spaces.
public static String Helper(String a)
{
a = a.toLowerCase();
a = a.replaceAll("\\s","");
return a;
}
//This method reverses a string
public static String Reverse(String a)
{
if(a.length() == 0)
{
return "";
}
String first = a.substring(0,1);
String reverse = Reverse(a.substring(1));
String finish = reverse + first;
return finish;
}
//This method tests if a string is a palindrome
public static String Palindrome(String a)
{
a = Helper(a);
String b = Reverse(a);
if( a.equals(b) )
{
return "yes";
}
else
{
return "no";
}
}
}