Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Palindrome [Stupid rules] (http://www.programmingforums.org/showthread.php?t=13925)

Wizard1988 Sep 5th, 2007 10:01 PM

Palindrome [Stupid rules]
 
Basically I have to write a program where the user can enter up to 30 characters and I have to check if the input is a palindrome. I did write this program and it works 100% however there is a catch... The professor wants us to store the input in char variables as in declare 30 chars and use that. What do you guys think is the least tedious way to accomplish this???

Thanks in advance.

ReggaetonKing Sep 5th, 2007 10:21 PM

How are you getting the input? Scanner class? Does the professor want you to capture each char the user enters and store in a char array?

Here what I've would done
:

//imports, class naming, etc...w/e!

public static void main(String[] args)
{
        java.util.Scanner scanner = new java.util.Scanner(System.in);
       
        char[] input = scanner.next().toCharArray();
        if(input.length > 30)
                throw new Exception("TOO BIG!!!");
       
        char[] reverseInput = new char[input.length];

        for(int x = 0, i = input.length -1; i > 0; i--, x++)
                reverseInput[x] = input[i];

        for(int x = 0; x < input.length; x++)
        {
                if(reverseInput[x] != input[x])
                {
                        System.out.prinln("Not a Palindrome!");
                        System.exit(0);
                }
        }
        System.out.prinln("It's a Palindrome!");
               
}

I did not compile it so there might be errors but that is along the lines of what I would do. I don't know if you're allowed to do this.

Wizard1988 Sep 5th, 2007 10:23 PM

Yes that would be the easy way to do it.... NO arrays are allowed. I can only use a string to get the input and then I have to store each character in a char.

Dameon Sep 5th, 2007 10:27 PM

Quote:

Originally Posted by Wizard1988 (Post 133408)
Yes that would be the easy way to do it.... NO arrays are allowed. I can only use a string to get the input and then I have to store each character in a char.

If that was his actual intention, I am truly disturbed.

ReggaetonKing Sep 5th, 2007 10:29 PM

Are you serious?!!? You actually have to DECLARE 30 char variables! Damn! Sorry dude... I don't have that much time. Honestly though, why would anyone need to do that!?

Wizard1988 Sep 5th, 2007 10:31 PM

I am too... I am assuming that his goal is to make us appreciate arrays ect...

King Sep 5th, 2007 10:45 PM

Yea that seems pretty stupid. My teacher in high school did something similar to get us to appreciate arrays, but there is better ways then wasting a night doing a retarded program like that lol. Just hand it in with 5 variables and add a comment at the end saying the rest will look similar haha.

crawforddavid2006 Sep 5th, 2007 10:59 PM

Quote:

Originally Posted by Wizard1988 (Post 133411)
I am assuming that his goal is to make us appreciate arrays ect...

or he doesnt know that arrays make things easy and more efficient

Wizard1988 Sep 7th, 2007 12:31 AM

Like a monkey I just typed out around 600 lines of code and this shit ain't working. :mad:

DaWei Sep 7th, 2007 12:48 AM

I've known some very good and dedicated teachers, but there's a reason for the stereotype: Those who can, do; those who can't, teach.


All times are GMT -5. The time now is 9:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC