palindrome

i cannot get what im doing wrong with my code

Page 1 of 1

10 Replies - 1581 Views - Last Post: 01 February 2010 - 08:01 PM Rate Topic: -----

#1 angel0lz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-July 08

palindrome

Post icon  Posted 04 August 2009 - 08:57 PM

 
import java.util.*;
import java.io.*;


public class Palindrome {


	public static void main(String[] args) {

	String[] tempArray=new String[80];
		String[] charArray=new String[80];
	charArray=args[0];

	if(isPalindrome(charArray)){
	System.out.println("It IS a palindrome!"); }
	else {
	System.out.println("It is NOT a palindrome!"); }
	System.out.println();
}



public static boolean isPalindrome(String string) {

	String testString=removeSpace(string);
	String reversedString=reverseArray(testString);
	return reversedString.equalsIgnoreCase(testString);
}


private static void copyArray(String[] dest, String[] source) {

	for(int x=0;x<=source.length;x++) {
	dest[x]=source[x]; }

}

private static void reverseArray(String[] string, String[] charArray) {
	
	copyArray(string,charArray);
	for(int x=0;x<=string.length;x++) {	
	string[x]=string[string.length-x]; }
		
	
}
	
	
protected static String removeSpace(String string) {
	
	int i, len = string.length();
	StringBuffer dest = new StringBuffer(len);
	char c;

	for (i = (len - 1); i >= 0; i--) {
	c = string.charAt(i);
		if (Character.isLetterOrDigit(c)) {
		dest.append(c);
		}
	}

	return dest.toString();
}

}


Palindrome
-Write a program that will accept a string of characters and determine whether the string is a palindrome

-The program should disregard blanks and case

-You may assume maximum length of 80, and no need to check whether the input is an English word


guys i cannot get what im doing wrong with this one.. any help will be much appreciated.. thanks ;)

Is This A Good Question/Topic? 0
  • +

Replies To: palindrome

#2 entro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-January 09

Re: palindrome

Posted 04 August 2009 - 09:42 PM

Looks like you're passing an empty charArray in main. Also, you can't pass an array to a method that accepts a string. Seems like you were looking at this website http://www.java2s.co.../Palindrome.htm I thought I came across this problem somewhere before.
Was This Post Helpful? 0
  • +
  • -

#3 gkgranada  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-July 08

Re: palindrome

Posted 04 August 2009 - 11:02 PM

your method is boolean, but you're trying to return a string? did you get the error message incompatible types?

i don't think anyway, that the string needs to be returned, so it's up to you if you want to make a new method getString or something.

This post has been edited by gkgranada: 04 August 2009 - 11:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: palindrome

Posted 05 August 2009 - 01:37 AM

public static boolean isPalindrome(String string) {

	String testString=removeSpace(string);
	String reversedString=reverseArray(testString);
	return reversedString.equalsIgnoreCase(testString);
}



That does return a boolean, .equals() method will return a true or false.
Was This Post Helpful? 1
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: palindrome

Posted 05 August 2009 - 05:43 AM

i have 2 snippets that checks for palindromes, one iterative and one recursive...but you might wanna modify it to ignore the cases and stuff
Was This Post Helpful? 0
  • +
  • -

#6 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: palindrome

Posted 05 August 2009 - 12:31 PM

I think you maybe way over complicating this question, not sure though I dont know all the details.

Do you even need to use array's? This seems very unnecessary seeing as you can go through a string very easily as just a string in it self, a simple
String str = "This is a string";

...

System.out.println(isPalindrome(str));//will return false because of the string, but make it a palindrome.

...

public static boolean isPalindrome(String str)
{
	for (int i = 0; i < str.length() - 1; i++)
	{
		if (str.charAt(i) != str.charAt(str.length() - 1 - i))
			return false;
	}
	return true;
}



this way you have not size limit, also its easier to see whats going on.
Was This Post Helpful? 0
  • +
  • -

#7 mysteriousfox  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 01-February 10

Re: palindrome

Posted 01 February 2010 - 12:40 PM

Can anyone reply with a full Palindrome program please??
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2728
  • View blog
  • Posts: 15,805
  • Joined: 24-September 08

Re: palindrome

Posted 01 February 2010 - 12:41 PM

No.

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#9 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: palindrome

Posted 01 February 2010 - 01:07 PM

View Postmysteriousfox, on 1 Feb, 2010 - 11:40 AM, said:

Can anyone reply with a full Palindrome program please??

Check out Sun's explanation of the String Class, and look at charAt() method. This might help you get your algorithm started, but don't look for someone to give you code without you writing any.
Was This Post Helpful? 0
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: palindrome

Posted 01 February 2010 - 01:43 PM

This thread is almost half a year old at this point. There's no need to bring back threads from the dead, especially just to ask for code
Was This Post Helpful? 0
  • +
  • -

#11 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: palindrome

Posted 01 February 2010 - 08:01 PM

I necropost for posterity. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1