Jump to content

How to solve palindrrome problem in java

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Hiche

Hiche

    Newbie

  • Members
  • Pip
  • 9 posts
The problem statement is:

Write a method isPalindromePhrase that accepts a string as argument and returns true or false
indicating if the string is a palindrome or not. In this version of the method, whitespaces, commas, and
apostrophes are ignored. For example, “Lonely Tylenol” and “Madam, I’m Adam” are both palindromes.
The signature of the method should be:
public static boolean isPalindromePhrase(String str)
Again here, you are not allowed to generate a new string in your implementation of this method.
Use this method to write a program Palindromes2 that takes an integer command line argument N
followed by N strings and prints the strings that are palindromes according to this extended definition.

My attempt at a solution:

public class Palindrome

{

	public static void main(String[] args)

	{

		int f = Integer.parseInt(args[0]); // parse integer N

		boolean result;

		String str = "";

		

		for (int i = 1; i <= f; i++)

		{

			str = args[i];

			result = isPalindromePhrase(str);

			if (result) // if value returned is true (IS palindrome)

				System.out.println(str + " is a palindrome.");

			else

				System.out.println(str + " is not a palindrome.");

		}

	}

	

	public static boolean isPalindromePhrase(String str)

	{

		if (str.length() <= 1) // a one-character string is always a palindrome

			return true;

			

		char rightChars, leftChars;

		int first = 0;

		int last = str.length() - 1;

		

		while (first < last)

		{

			leftChars = str.charAt(first); // characters from the left (moving from left to right)

			leftChars = Character.toLowerCase(leftChars); 

			rightChars = str.charAt(last); // characters from the left (moving from right to left)

			rightChars = Character.toLowerCase(rightChars);

			

			if (leftChars == ' ' || leftChars == '\'' || leftChars == ',')

			{

				leftChars = str.charAt(first++);

			}

			if (rightChars == ' ' || rightChars == '\'' || rightChars == ',')

			{

				rightChars = str.charAt(last--);

			}

			

			if (leftChars == rightChars)

			{

				first++;

				last--;

			}

			else

				return false;

		}

		return true;

	}

}

I tried so many other ways but nothing is working. Can anyone solve this? I have the assignment due for tomorrow and this is the only one that I need to solve.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I would create a new string from the string to test, upper-casing it and stripping out all non-characters. Then I would reverse that string and comparing it to the new string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hiche

Hiche

    Newbie

  • Members
  • Pip
  • 9 posts
I did that, but the thing is, as I learned today, our instructor wants us to take each character by character then compare the first one to the last one, take the next first character and next last character and compare etc etc and such. Also, in this program we are disregarding spaces, commas and apostrophes. Therefore, "Madam, I’m Adam" should be considered a palindrome. We are limited to loops, conditionals, character/string methods, methods, and command-line arguments.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
if (leftChars == ' ' || leftChars == '\'' || leftChars == ',')             
{ 
                leftChars = str.charAt(first++);
I would use a While loop instead of if here. That way, things like " ," don't screw you up.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Hiche

Hiche

    Newbie

  • Members
  • Pip
  • 9 posts
So just replace if with while? I did that but no luck. Is there something I am missing? The while loop will check if the character is not a letter and keeps looping until reaching a letter (with the characters moving to the left in the case of leftChars or to the right in the case of rightChars).

Any help would be greatly appreciated. Thank you.

---------- Post added at 12:05 AM ---------- Previous post was at 12:00 AM ----------

Never mind. Got it. Thanks.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The while loops will need to include a lowercase call. That, or compare .lowerCase values.

You also might want to show the values that failed the check in your else clause before returning false, to help with debugging.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users