![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
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.
__________________
JG-Webdesign |
|
|
|
|
|
#2 |
|
Sexy Programmer
|
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 would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#3 |
|
Professional Programmer
|
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.
__________________
JG-Webdesign |
|
|
|
|
|
#4 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
If that was his actual intention, I am truly disturbed.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
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!?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#6 |
|
Professional Programmer
|
I am too... I am assuming that his goal is to make us appreciate arrays ect...
__________________
JG-Webdesign |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 369
Rep Power: 0
![]() |
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.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#8 |
|
Expert Programmer
|
or he doesnt know that arrays make things easy and more efficient
__________________
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
Like a monkey I just typed out around 600 lines of code and this shit ain't working. :mad:
__________________
JG-Webdesign |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple, contrived programming exercise | DaWei | Coder's Corner Lounge | 16 | Jun 14th, 2006 10:56 AM |
| Testing for a palindrome using std::string | Jessehk | C++ | 9 | May 3rd, 2006 1:55 AM |
| if the character string is palindrome ? | eax | Assembly | 3 | Apr 8th, 2006 12:16 PM |