Hi! This is my first time posting on this forum as alot of you have helped me with programs earlier in the year, I am currently in AP Computer Science at my school and we have a project to do where we have to create a program to recognize palindromes through means of recursion. I can't figure out how to make the program ignore capital letters and punctuation like commas, but I did manage to get it to recognize the spaces. Here is my code:
Any help is appreciated. thanks!
import java.util.*;
import java.lang.*;
public class Palindrome
{
public static void main(String[] args)
{
String str;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a word to see if its a Palindrome; ");
str= scan.nextLine();
System.out.println();
if(isPalindrome(str,0, str.length()-1))
System.out.println( str + " is a palindrome");
else
System.out.println(str + " is not a palindrome");
}
public static boolean isPalindrome(String str,int low, int high)
{
if(str.charAt(low)== 32) {
low++; }
if(str.charAt(high)== 32) {
high--;}
if(high <= low)
return true;
else if (str.charAt(low)!= str.charAt(high))
return false;
else
return isPalindrome(str,low+1,high-1);
}
}
Any help is appreciated. thanks!