Link to home
Start Free TrialLog in
Avatar of warc_renegadex
warc_renegadexFlag for Australia

asked on

To code a program to check whether it is a palindrome or not

I want to desgin a  program is to input several strings, one per input line, and determine whether each is a palindrome.  

The first input item, an integer number, tells how many strings the program must analyse.

 After this input value, each string is entered on a separate input line followed by program's analysis of the string.A palindrome is a string that reads the same forwards and backwards, disregarding punctuation and the distinction between uppercase and lowercase letters. Examples are:
· the name:       Bob
· the word:       noon
· the word:       RADAR
· the name:      Hannah
· the sentence:   Madam, I'm Adam
· the sentence attributed to Napolean when exiled to Elba:
Able was I, ere I saw Elba

its a very good concept to learn, i just want to know how to code so I can prepare to more hardcore stuff like this.
ASKER CERTIFIED SOLUTION
Avatar of samers
samers

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sasha_Mapa
Sasha_Mapa

I assume that samer's comment won't satisfy your teacher as he probably wanted YOU to write the actual method that checks whether the string is a palindrom. Anyway, we don't do people's homework here and this sounds a LOT like homework!
;) yeah
Avatar of warc_renegadex

ASKER

thanks samer for your answers that was helpful. Yes i do agree with sasha or ovid that you should just provide help but not do all the homework for me or others.

Just because i have not been checking this website does not mean I'm waiting for better answers to come by i was got up in a lot of other work as well. During that time I've written up the classes and all the methods which are working okay but not perfect but I'm getting there. I was taking my time to understand what samers has said. Well I'm no expert like you sasha but yes you are a pro, I've don't have the luxury of time or access to good computing faclities like yourself
to learn what is required. I've a very demanding workload on other units such as electronic commerce. java is one those units that is takingmy time in other units.

If i wanted you to do my homework for me I could have just said please help with my assignment now and be like hassling everyone and asking more questions to get time. but i place no time limit on this answer. I like varietys.

i  need to learn how to do this because later we are going to be tested on this concept and need to know the basics of how it works to use it.
This is what have taken my time to do this from samers work. I've check that it works but still not sure I'm no pro but please you are free to suggest anything to it.


public class Pal
{
    /** A StringBuffer object is used to manipulate the arguments */

    public static StringBuffer line = new StringBuffer();
    public static String sample;

    /** P() is a convenience function to simplify printing */

    public static void P(String z)
    {
        System.out.println(z);
    }
   
    public static void main( String s[] )
    {
        /**
            Test for arguments.
         
            s.length == 0 tests for the existence of
            an argument to java Pal
        */

        if (s.length == 0)
        {
            P("Usage: java Pal String1 [ String2 ... String(n) ]");
            System.exit(1);
        }
        else
        {
            /**
                "line.append(s[i])" concatenates all command line
                arguments and removes all spaces, leading and
                internal, at the same time.
            */

            for ( int i = 0; i < s.length; i++)
            {
                line.append(s[i]);
            }

            /**
                "sample = line.toString()" converts the sanitized
                StringBuffer object "line" to a String object (sample)
            */

            sample = line.toString();

        }

        /**
            The following line creates the Palindrome object
        */

        Palindrome p = new Palindrome(sample);



    }
}


class Palindrome
{
    /**
       The Palindrome constructor calls removePunc(), convertToUpper(),
       reverseIt(), and printPal().
    */

    /** P() is a convenience function to simplify printing */

    public static void P(String z) { System.out.println(z); }

    Palindrome(String t)
    {
        String temp1 = new String();
        String temp2 = new String();
        temp1 = removePunc(t);
        temp1 = convertToUpper(temp1);
        temp2 = reverseIt(temp1);
        printPal(t, temp1, temp2);
    }

    /**
       printPal(String t, String s1, Strings2) tests strings
       and prints the results.
    */

    void printPal(String t, String s1, String s2)
    {
        if ( s1.equalsIgnoreCase(s2))
        {
            P(t + " is a palindrome");
        }
        else
        {
            System.out.println(t + " is not a palindrome");
        }
    }
       
    /**
       convertToUpper() converts all alphbetic characters to uppercase.
       This function could have been eliminated and place inline in the
       constructor, but for the sake of consistency, it became a function.
       This function returns a string of all UPPER CASE.
    */

    String convertToUpper( String str )
    {
        String temp;  
        temp = str.toUpperCase();
        return(temp);
    }

    /**
       removePunc() removes all non-alphanumeric characters
       and returns the new string.
    */
   
    String removePunc( String s)
    {
        int count = 0;
        int temp;
        StringBuffer buf = new StringBuffer();
        char t;
        String rtn;

        for ( count = 0; count < s.length(); count++)
        {
            t = s.charAt(count);
            if (Character.isLetterOrDigit(t))
            {
                buf.append(t);
            }
        }

        rtn = buf.toString();
        return(rtn);
    }

    /**
       reverseIt() reverses the characters in a string
       and returns the reversed string.
    */

    String reverseIt( String s )
    {
        StringBuffer temp = new StringBuffer();
        String str;
        int count;

        for ( count = 0; count < s.length(); count++)
        {
            temp.append(s.charAt(s.length() -1 - count));
        }
        str = temp.toString();
        return(str);
    }    
       

}
Your assignment isn't even a Java assignment, it's just programming. It's something they give when you learn programming for the first time, and if you know how to implement it in any language, it will be easy to implement it in any other language.
Anyway, about your actual code:
A. >> if (s.length == 0) - won't compile, String.length() is a method, to call a method you add '()' after the name of the method even if there are no arguments.
B. >> "line.append(s[i])" concatenates all command line arguments and removes all spaces, leading and internal, at the same time.
No if doesn't!!! StringBuffer.append(char) just appends the given character to the end of the StringBuffer.
C. >> /** P() is a convenience function to simplify printing */
This is just silly. Call System.out.println, it will make your code more readable.
D. >> return(rtn);
Return is not a method, you just need to type "return rtn;", or even easier, "return buf.getString();". Same for all the other places when you use return as a method.
E. >> String reverseIt( String s )
Way to go!! You wrote it yourself!!
F. Generally, your program is not designed very well, it is neither object oriented actually nor is it procedural. You do all the important work in Palindrome's constructor, that's not the way to start learning Java or OO design.
G. Think man!! For crying out loud, this is such a simple algorithmical task!
All you have to do is check whether every character at location n is equal to the character at string.length()-1-n and if it is true for n=0 to n=string.legnth()/2 (take a minute to think why we don't need to check for all n's, 0 through string.length()-1) then the string is a palindrome. As for design, this simple method does not deserve a class of its own, it should just be a static method in some class named StringUtilities along with methods to return a reversed a string, return a string with all non digit/letter characters removed etc.

Anyway, you have a wrong attitude for studies. I suppose that if you are learning Java it's not because you HAVE to like they make you study literature in school so you just want to get it over with. In University the studies are for you, not for the professors, they already (not always actually :-) ) know the material. You are the one who is going to go to a job interview later and when they ask you to write a method that checks whether a string is a palindrome, you are going to write them 100 lines of code for an hour instead of the 5 lines and 2 minutes it actually takes.

Btw, I'm not a pro yet, I'm just 18 and finishing highschool.

Good luck and please think over about what you are actually doing.
hey sama thanks for your comment i greatly appreciate what you have just said. Well i'm trying really my best to make it Object orientated with methods. object orientated is something i'm working with. I was referring the pro to sasha not you. I'll take your criticsm although was quite strong. I've still got a lot to learn and that is why i'm coming here for help. sure i've got my weakness i'm sure wanna find out and let someone like you suggest some ways to help improve and become better at what i'm doing.

i totally agree with you that i should display the user to enter the palindrome instead of running it and entering the palindrome at the same time.

Well i know its simple for you because maybe you have more experience, i'm kind of trying to work my way around java at the moment, its a steep learning curve, its like when you learn windows and you use unix. I was learning pascal before, pascal was alright its just that java was a little foreign to me. Yes i guess i'm not taking things as they should be, should relax and stop trying to get over and done with but you got to realise when you get to college or university no body spoon feeds you, no body gives a crap what you do eg professors sometimes don't know jack. I'm going to try to be efficent as possible in doing this sort of things. i know 100 lines of code with no get noway if you are smart enough to come up with 5 lines and 2 minutes.

do you have an icq number, my is icq # 22126414
Ok, you got me really confused...whom are you talking to, samers or me? :-)