Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-04-17
8
Medium Priority
?
607 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:warc_renegadex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Accepted Solution

by:
samers earned 195 total points
ID: 2725597
I thing it's some thing easy to do
all what u do is to store ur String into a StringBuffer object after u read it
lets say u have a DataInputStream .

DataInputStream in = new DataInputStream(System.in);

then read the input line by line into a string:

String str = in.readLine();

after that u try to remove from that String all punctuation characters.
by number of subString and concatinations on the String, so the String is free from those Characters.

then construct a StringBuffer from that String;

StringBuffer sBuf = new StringBuffer(str);

then have a new String from the reverse of the String stored in the StringBuffer.

Strin reverse = sBuf.reverse();

after that u can combare sBuf.toString() with the String stored in reverse object.
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2725857
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!
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2726476
;) yeah
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:warc_renegadex
ID: 2731390
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.
0
 

Author Comment

by:warc_renegadex
ID: 2731408
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);
    }    
       

}
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2732124
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.
0
 

Author Comment

by:warc_renegadex
ID: 2732206
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
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2732260
Ok, you got me really confused...whom are you talking to, samers or me? :-)
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question