12 Replies - 3054 Views - Last Post: 11 July 2011 - 07:53 AM Rate Topic: -----

#1 Racer X  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 09

check string palindrome

Post icon  Posted 02 November 2009 - 11:45 PM

c
lass Palindrome
{
	static boolean checkpalindrome(char[] A)
	{
		boolean palin=true;
		for(int i=0;i<=A.length/2;i++)
		{
			if (A[i]!=A[A.length-i-1])
			{
				palin=false;
				break;
			}
		}
		return palin;
	}
	public static void main(String args[])
	{
		BufferedReader bf=new BufferedReader(new inputStreamReader(System.in));
		System.out.print("Enter a word");

		String inpStr=bf.readline(); 
		inpStr=inpStr.toUpperCase();
		char[] inpass=inpStr.toCharArray();
	
		if(checkpalindrom(inpass))
		System.out.print("Yes the string is palindrome");
		else
		System.out.print("No, the string is not palindrome");

	}
}


//i get 4 errors and dont know the problem can anyone help me

This post has been edited by JackOfAllTrades: 10 July 2011 - 07:55 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: check string palindrome

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: check string palindrome

Posted 03 November 2009 - 12:33 AM

:code:

Also please post the errors that you are getting along with the Line of code that it is reffering to.

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 Racer X  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 09

Re: check string palindrome

Posted 03 November 2009 - 12:40 AM

error is
Palindrome.java:18: cannot find symbol
it shows the same 3 errors
Was This Post Helpful? 0
  • +
  • -

#4 Aligator301  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 01-November 09

Re: check string palindrome

Posted 03 November 2009 - 12:53 AM

Hey buddy,

I can't see the file you have there but you're missing the import at the top, so that is a bad start (you need to import the packages or files you intend to use)

Also, your spelling and capitalization is letting you down!
you spelled your own function incorrectly, error
you used an 'i' for inputStreamReader instead of InputStreamReader

Go and get yourself a copy of Netbeans (http://www.netbeans.org/) to do your work in so that it can highlight your errors in the code as you work or if you can wait, get IntelliJ 9 basic edition (which will be free)



import java.io.*;

public class Palindrome
{
	public static boolean checkpalindrome(char[] A)
	{
		// always sanity check.
		if (A!=null && A.length % 2 == 0)
		{
			for (int i=0; i<=A.length/2; i++)
			{
				if (A[i]!=A[A.length-i-1])
				{
					return false;
				}
			}
			return true;
		}
		
		return false;
	}
	
	public static void main(String args[])
	{
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter a word");

		String inpStr=null;

		try
		{
			inpStr = bf.readLine();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		if (inpStr!=null && inpStr.length()>0)
		{
			inpStr=inpStr.toUpperCase();
			char[] inpass=inpStr.toCharArray();

			if (checkpalindrome(inpass))
				System.out.print("Yes the string is palindrome");
			else
				System.out.print("No, the string is not palindrome");
		}
		else
		{
			System.out.print("input not well formed.");
		}


	}
}





A.
Was This Post Helpful? 0
  • +
  • -

#5 y2161994  Icon User is offline

  • New D.I.C Head

Reputation: 15
  • View blog
  • Posts: 34
  • Joined: 08-July 11

Re: check string palindrome

Posted 10 July 2011 - 04:23 AM

this program is only syntatically correct.logically it is not correct.when I gave radar,this produce output that,"No, the string is not palindrome".
But radar is a palindrome string.
Was This Post Helpful? 0
  • +
  • -

#6 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: check string palindrome

Posted 10 July 2011 - 07:45 AM

Why are you using such as complex method for determining if it is a palindrome?
All you would have to do is reverse the string and see if it equals the original string.
public boolean checkPalindrome(String toCheck){
    String reverse = new StringBuffer(toCheck).reverse().toString();
    if(toCheck.equals(reverse)){
        return true;
    }
    return false;
}


Or do you have to use a char array for some reason?

Note: I coded that from the hip straight into the post. I might have some minor syntactical errors.
Was This Post Helpful? 1
  • +
  • -

#7 y2161994  Icon User is offline

  • New D.I.C Head

Reputation: 15
  • View blog
  • Posts: 34
  • Joined: 08-July 11

Re: check string palindrome

Posted 11 July 2011 - 04:07 AM

Quote

Thank you.
Your code works.
I m very happy after doing this program .


Quote

Thank you.
Your code works.
I m very happy after doing this program .

Was This Post Helpful? 0
  • +
  • -

#8 javabrijon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 27-October 09

Re: check string palindrome

Posted 11 July 2011 - 04:17 AM

Dont you have to close your BufferedReader after its finished?

P.s. if you read below under related topics there are a few more topics on Palindromes which could give you code hints.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: check string palindrome

Posted 11 July 2011 - 04:52 AM

I actually prefer the opening poster's solution to the reverse() method. The code becomes clearer when you use two variables for the loop through.

boolean isPalindrome(String str) {
  char[] ch = str.toLowerCase().toCharArray();
  int start = 0;
  int end = ch.length - 1;

  while (end > start) {
    if (ch[start] != ch[end])
      return false;
    start++;
    end--;
  }
  return true;
}

This post has been edited by cfoley: 11 July 2011 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: check string palindrome

Posted 11 July 2011 - 05:28 AM

I like the way racer x was thinking about doing it in the first place. There really isn't any more code lines, and it is faster than reversing the String. I don't see how it can be any simpler.

	public boolean isPalindrom( String word ) {
		for ( int i = 0; i < word.length() / 2; i++ ) 
			if ( word.charAt( i ) != word.charAt( word.length() - i - 1 ) )
				return false;
		return true;
	}


This post has been edited by CasiOo: 11 July 2011 - 05:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: check string palindrome

Posted 11 July 2011 - 05:39 AM

If we are talking lines of code, the reverse() method IS smaller:

	boolean isPalindrome(String str) {
		return new StringBuffer(str).reverse().toString().equals(str);
	}


But I don't like it because it requires a relatively in-depth knowledge of the Java API for a task that's very simple with a small loop. I don't like all the arithmetic in the original solution. It's confusing to look at and if there was a bug, it could take some figuring out. The arithmetic is easily factored out by using two variables. It looks like it should be slower, but actually there are less calculations going on.
Was This Post Helpful? 1
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: check string palindrome

Posted 11 July 2011 - 07:18 AM

@cfoley: Being a little anal here, the StringBuilder class is more efficient b/c it isn't synchronized, while StringBuffer is. :)
Was This Post Helpful? 2
  • +
  • -

#13 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: check string palindrome

Posted 11 July 2011 - 07:53 AM

You know, I looked up the API for StringBuilder and then typed StringBuffer. :(
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1