5 Replies - 1477 Views - Last Post: 27 January 2013 - 04:52 PM Rate Topic: -----

#1 afrodaddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 13

Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 02:25 PM

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:

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Palindrome Recursion Help Needed

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 02:27 PM

Simple,
palindromeString.toLowerCase();

and
palindromeString.replace("Punctation and things you want to ignore go here","");

This post has been edited by natecat: 27 January 2013 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 afrodaddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 13

Re: Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 02:29 PM

Where in the code would that go exactly? Thanks for the reply!
Was This Post Helpful? 0
  • +
  • -

#4 ccdan  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 09-December 12

Re: Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 04:17 PM

Well, if you want to ignore character case or special characters, you should do that before testing whether the string is a palindrome or not, modifying the string before you pass it to that method.
Let's say you have String st = "AbcDEf"; you get rid of (uppers) case with st = st.toLowerCase(); or vice-versa with toUpperCase. If you want to get rid of certain special characters, you can use some loops and remove the unwanted stuff (or use regular expression, but if you're beginner, it's probably not the best idea)

As for recursion, you don't have to ask for beginning/ending index values, the string alone is sufficient. you should do the following:
1. test for the base case (check whether the string passed is size 0 or 1 and return true if so
2. compare the first character and the last character of the string and if true, return the name of the method itself (isPalindrome) - this is the recursive call - having as parameter a subtring that goes one step inside (from index 1 to length()-1)
3. return false at the very end of the code..
Was This Post Helpful? 0
  • +
  • -

#5 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 04:50 PM

View Postafrodaddy, on 27 January 2013 - 09:29 PM, said:

Where in the code would that go exactly? Thanks for the reply!


In the palindrome method.
Was This Post Helpful? 0
  • +
  • -

#6 afrodaddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-January 13

Re: Java Palindrome Recursion Help Needed

Posted 27 January 2013 - 04:52 PM

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.toLowerCase().replace(',','!','.').charAt(low)!= str.toLowerCase().replace(',','=','!','.').charAt(high) )
 return false;
else
 return isPalindrome(str,low+1,high-1);    
   }   
}


This is how it looks, the ignore case has worked, but the replace im not sure if I am doing it right but it is giving me this error:

Error: no suitable method found for replace(char,char,char)
method java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence) is not applicable
(actual and formal argument lists differ in length)
method java.lang.String.replace(char,char) is not applicable
(actual and formal argument lists differ in length)
File: E:\Palindrome.java [line: 37]
Error: no suitable method found for replace(char,char,char,char)
method java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence) is not applicable
(actual and formal argument lists differ in length)
method java.lang.String.replace(char,char) is not applicable
(actual and formal argument lists differ in length)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1