The Artima Developer Community
Sponsored Link

Java Answers Forum
please please help - palindrome program

4 replies on 1 page. Most recent reply: Aug 27, 2003 1:57 AM by Alex

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Alex

Posts: 4
Nickname: alex107
Registered: Aug, 2003

please please help - palindrome program Posted: Aug 26, 2003 4:51 AM
Reply to this message Reply
Advertisement
hi,
i have to create this program that reads in a string and then determines if it is a palindrome or not (eg. dad, radar). but it has to ignore special characters, operators(*,+,& etc.). i have the program all working but it doesn't ignore the special characters. i thought i made the program ignore it by using the if ((letter >= 'a') && (letter <= 'z')) part in my code, thanks in advance for any help


public class Palindrome
{
public static void main(String[] args)
{
System.out.println("Please enter a sentence: ");
String str = SavitchIn.readLine();

if (palindrometester(str))
System.out.println("The input is a palindrome.");

else
System.out.println("The input is not a palindrome.");
}

private static boolean palindrometester(String str2)
{
String original = "";
String reverse = "";
char letter;
char letter2 ;

str2 = str2.toLowerCase();
for (int i = 0; i < str2.length(); ++i)
{
letter = str2.charAt(i);
//letter2 = str2.charAt(str2.length() - i - 1);

if ((letter >= 'a') && (letter <= 'z'))
{
original = original + letter;
reverse = reverse + letter2;
}
}

return (original.equals(reverse));
}
}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: please please help - palindrome program Posted: Aug 26, 2003 8:22 AM
Reply to this message Reply
I just wrote a method which will remove all the special chars from your String. Why don't you pass your string through that and then call the palindrometester method. So you don't have to do any of the special char checking in there...

public static String removeSpecial(String pala){
  String buffer = "";
  char tempChar;
  for (int i = 0; i < pala.length(); ++i){
    tempChar = pala.charAt(i);
    if ((tempChar < 'a') || (tempChar > 'z'))
      continue;
    buffer += tempChar;
  }
  return buffer;
}

David

Posts: 18
Nickname: malven
Registered: Jul, 2003

Re: please please help - palindrome program Posted: Aug 26, 2003 7:14 PM
Reply to this message Reply
Simply comparing against the range 'a'-'z' will omit uppercase letters, which may not be what you want. Why not use java.lang.Character.isLetter() instead?

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: please please help - palindrome program Posted: Aug 26, 2003 9:17 PM
Reply to this message Reply
Thanx David for your suggestion. You have a point. The modified version is here with Character.isLetter() function in action :-)

  public static String removeSpecial(String pala){
    String buffer = "";
    char tempChar;
    for (int i = 0; i < pala.length(); ++i){
      tempChar = pala.charAt(i);
      if (!Character.isLetter(tempChar))
        continue;
      buffer += tempChar;
    }
    return buffer;
  }

Alex

Posts: 4
Nickname: alex107
Registered: Aug, 2003

Re: please please help - palindrome program Posted: Aug 27, 2003 1:57 AM
Reply to this message Reply
thank you both

Flat View: This topic has 4 replies on 1 page
Topic: search a large text file for a word Previous Topic   Next Topic Topic: submit form on another site and take my users to external confirmation page

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use