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.
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.
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
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.
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.
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" );
}
}
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!
Sorry. I meant by using a char array. I am not yet used to this String stuff. I am really a beginner.
Thanks.
> 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 :)
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.
> 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?
> > 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.
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?
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
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.
> > 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.