PALINDROME

Is it *extremely* difficult to develop a palindrome program with Java? Anyone can show me just a simple pseudocode or algorthm. Not too detail though. Just a brief description.

Thanks.

[197 byte] By [kraton_mayaa] at [2007-11-27 11:29:48]
# 1

Iterative version.

Have a pointer to the first letter

Have a pointer to the last letter

Loop while first pointer is less than last pointer {

If two letters are not equal {

return false

}

Increment first pointer

Decrement last pointer

}

Return true

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 2

Impressive.

I'll try that one as my homework. Are you suggesting an array?

sknahT

p/s That pointer stuff gives me the creep already.

kraton_mayaa at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 3

> Are you suggesting an array?

Do you see anywhere in my pseudocode the mention of an array?

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 4

BTW what exactly do you mean by palindrome program. Is it supposed to examine a String and determine if it is palindrome or not? If so, my pseudocode will do that. If not please explain what you need in more detail.

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 5

class Tester

{

public boolean test( String trial )

{

String lower = trial.toLowerCase();

StringBuffer azBuffer = new StringBuffer();

for ( int j=0; j < lower.length(); j++ )

{

char c = lower.charAt(j);

if ( c >= 'a' && c <= 'z' )

azBuffer.append( c );

}

String forward = azBuffer.toString();

String backward = azBuffer.reverse().toString();

if ( forward.equals( backward ) )

return true;

else

return false;

}

}

public class PalindromeTester

{

public static void main ( String[] args )

{

Tester pTester = new Tester();

String trial = "A man, a plan, a canal, Panama!" ;

if ( pTester.test( trial ) )

System.out.println( "Is a Palindrome" );

else

System.out.println( "Not a Palindrome" );

}

}

fastmikea at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 6

fastmike, you just need to stop, lol.

TuringPesta at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 7

JEEBUS CHRIST fastmike!

How many fucking times do you have to be told? Don't provide code. It teaches them nothing and we are not impressed by your coding skills. In my opinion you are nothing but an idiot!

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 8

Sorry. I meant by using a char array. I am not yet used to this String stuff. I am really a beginner.

Thanks.

kraton_mayaa at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 9

Thanks mike.

But you are just too fast. But hey, an elegant code though.

kraton_mayaa at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 10

Using a char array will be exactly the same. The two pointers will be indicies into your array.

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 11

> Thanks mike.

>

> But you are just too fast. But hey, an elegant code

> though.

That is why they call me fastmike. thinking of changing to supermike(i got this nickname from flounder :)

fastmikea at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 12

ugh. you dont need a char array and mikes code is a bad idea.

TuringPesta at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 13

But it isn't an elegant solution!

Here's one way to make it better.

return forward.equals( backward ) ;

But to make it even more elegant you don't even need all those other Strings and StringBuffers.

floundera at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 14

> ugh. you dont need a char array and mikes code is a

> bad idea.

what what? how how? where where? Oh please my code is simple and a noob can easily understand how to program a plaindrome. OP do you want to extract the output for the plaindrome? or just to see if its a plaindrome or not?

fastmikea at 2007-7-29 16:30:09 > top of Java-index,Java Essentials,New To Java...
# 15

> > Thanks mike.

> >

> > But you are just too fast. But hey, an elegant

> code

> > though.

>

> That is why they call me fastmike. thinking of

> changing to supermike(i got this nickname from

> flounder :)

No, you got the nickname fuckwit from me.

floundera at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 16

"fuckwit". haha, now thats funny.

TuringPesta at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 17

You guys are fun and great. Thanks to you all.

Is there any other way than using a char array? Please don t suggest String to me. I am not even a good C student!

By the way, the previous snippet, does it need swapping?

kraton_mayaa at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 18

compare this to your insanity:

public class Palindrome{

public static void main(String[] args){

String text = "abcdefg";

for(int i = 0; i < (text.length() / 2); i++){

char front = text.charAt(i);

char back = text.charAt(text.length() - i - 1);

System.out.println("Front: " + front + " , Back: " + back);

}

}

}

hint: if front != back return false

TuringPesta at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 19

Strings really are char arrays. Perhaps you can tell us if there is another way to do it.

DO NOT look at fastmikes code. Basically it is **** and you will learn nothing. I gave you a perfectly good algorithm in reply #1. Try to implement it and actually leran something.

floundera at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 20

I will.

Thanks.

kraton_mayaa at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...
# 21

> > ugh. you dont need a char array and mikes code is

> a

> > bad idea.

>

> what what? how how? where where? Oh please my code is

> simple and a noob can easily understand how to

> program a plaindrome. OP do you want to extract the

> output for the plaindrome? or just to see if its a

> plaindrome or not?

you can do either with a single line of code... So yes, your code is bad.

It's errorprone simply because it's way too complicated.

jwentinga at 2007-7-29 16:30:13 > top of Java-index,Java Essentials,New To Java...