469,366 Members | 2,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

palindrome program

24
hi,iam learning perl for the past two weeks and i was told to do a palindrome program.can any one help me with the code please.
Aug 28 '07 #1
16 15865
KevinADC
4,059 Expert 2GB
Help? Yes. Do it for you? No. Post the code you have written so far.
Aug 28 '07 #2
humaid
24
Expand|Select|Wrap|Line Numbers
  1. do {
  2.     print "Type a word or phrase: ";
  3.     $line = <>;
  4.     @letters = split //, $line;
  5.     $reverse = join "", reverse @letters;
  6.  
  7.     if (@letters == 1) {
  8.         print "One letter palindrome, trivial!\n";
  9.     }
  10.     elsif ($reverse eq $line) {
  11.         print "This is a palindrome.\n";
  12.     }
  13.     else {
  14.         print "Not a palindrome.\n";
  15.     }
  16. } while ($line);
  17.  
this is the code i have done so far.the problem is every word i type,it shows it is palindrome.
Aug 30 '07 #3
miller
1,089 Expert 1GB
Don't forget to chomp the $line variable to remove the return character.

Also, reverse on a scalar does a string reversal. So the split and join operation is extra work.

Expand|Select|Wrap|Line Numbers
  1. chomp($line);
  2.  
- Miller
Aug 30 '07 #4
KevinADC
4,059 Expert 2GB
Also, reverse on a scalar does a string reversal. So the split and join operation is extra work.
using split and join might be a requirement for the assignment. Someone posted the exact same assignment on another forum a couple of days ago. But the code on the other forum wasn't even close to getting it right.
Aug 30 '07 #5
numberwhun
3,503 Expert Mod 2GB
here is the program .see whether it works out
nimisha,

While I know you think you are being helpful, simply providing the full script to the user, you are actually hindering their learning process. It is the policy of thescripts.com forum NOT to provide the answers to people's school work. Instead, we request that they post the code they are working on and help them to get it correct for what they are trying to do.

In the future, please work with the users code and refrain from the urge to just post the code they need.

Regards,

Jeff
Aug 30 '07 #6
humaid
24
tank you for the code,but the problem is, in the o/p after enetring the string nothing shows.that is the result is not known whether the enetred string is palindrome or not.
Aug 30 '07 #7
humaid
24
thank you guys for the support,ive solved the problem.here is the code for palindorme.

Expand|Select|Wrap|Line Numbers
  1. #!/user/bin/perl
  2. $a=<STDIN>;
  3. chomp($a);
  4.  
  5. @aa=split("",$a);
  6. print "@aa\n";
  7.  
  8. @b=reverse (@aa);
  9. print "@b\n";
  10.  
  11. $c=join("",@aa);
  12. $d=join("",@b);
  13.  
  14. if ($c eq $d) {
  15.     print "the given string is palindrome\n";
  16. } else {
  17.     print "not a palindrome\n";
  18. }
  19.  
Aug 30 '07 #8
numberwhun
3,503 Expert Mod 2GB
thank you guys for the support,ive solved the problem.here is the code for palindorme.
Good job! Thank you for posting your working solution and letting us know you solved your issue!

Regards,

Jeff
Aug 30 '07 #9
KevinADC
4,059 Expert 2GB
here is the program .see whether it works out

All that for a simple palindrome script? Well, it does adhere to the TIMTOWTDI philosophy of perl coding.
Aug 30 '07 #10
Kelicula
176 Expert 100+
Or even this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. my $input =<STDIN>; # get the input
  6. chomp($input); # chop \n off
  7.  
  8. $input =~ s/\\s//g; # substitute spaces for nothing
  9.  
  10. if($input eq reverse($input)){ # compare
  11.  
  12. print "This is a palindrome";
  13. }
  14. else{
  15. print "This is not a palindrome";
  16. }
  17.  
  18.  
No real need for arrays.
Of course it's very good to know how to manipulate arrays like you are doing...

:)
Sep 2 '07 #11
Kelicula
176 Expert 100+
Even better!! Hehehe (I'm having fun with this one)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w
  2. use strict; # I'm using strict now guys! :)
  3.  
  4. my $input =<STDIN>;
  5. chomp($input);
  6.  
  7. $input =~ s/\\s//g;
  8.  
  9. $input eq reverse($input)? print "This is a palindrome" : print "This is not a palindrome";
I made use of the conditional statement where a comparison is followed by a "?"
the the code to be executed if true, followed by ":" and the code to be executed if NOT true. Very handy, only works for if else though. Can't have multiple things. elsif, elsif etc...

Notice that in PERL "else if" is actually written with one "e" missing. "elsif"
No typo.
Sep 2 '07 #12
KevinADC
4,059 Expert 2GB
Very handy, only works for if else though. Can't have multiple things. elsif, elsif etc..
Actually the ternary operator can have multiple conditions. Example:

Expand|Select|Wrap|Line Numbers
  1. my @nums = (23, 34, 77, 100, 61, 200, -3);
  2. for (@nums) {
  3. my $return = ($_ > 99) ? "$_ is over 99" :
  4.                  ($_ > 49) ? "$_ is over 50 under 100" :
  5.                  ($_ > 25) ? "$_ is over 25 under 50" :
  6.                  ($_ > 0) ? "$_ is over 0 under 26" : "$_ is under zero";
  7. print $return,"\n";
  8. }  
Sep 2 '07 #13
Kelicula
176 Expert 100+
here is the program .see whether it works out
It doesn't work. You have not loaded your subroutine into memory when you call on it at line 7. Also you have a typo (?) in your sub. ie:

Expand|Select|Wrap|Line Numbers
  1. my $leftchar = lc substr($string, $ctr_1, 1);
  2.  
The scalar $string hasn't been initialized.

Very interesting idea though. Cycle through the word comparing the first letter to the last, the second to the next to last etc... At least once you find an inequality the program will stop, NOT actually having to compare the whole statement. Could come in handy for comparing a very large file/scalar/array ect...

One other thing Wikipedia states that palindrome can be words, or phrases.
Sep 2 '07 #14
Kelicula
176 Expert 100+
Actually the ternary operator can have multiple conditions. Example:

Expand|Select|Wrap|Line Numbers
  1. my @nums = (23, 34, 77, 100, 61, 200, -3);
  2. for (@nums) {
  3. my $return = ($_ > 99) ? "$_ is over 99" :
  4.                  ($_ > 49) ? "$_ is over 50 under 100" :
  5.                  ($_ > 25) ? "$_ is over 25 under 50" :
  6.                  ($_ > 0) ? "$_ is over 0 under 26" : "$_ is under zero";
  7. print $return,"\n";
  8. }  

What are the disadvantages if any to using the ternery operator?
Seems like a very good way to run comparisons.

Thanks Kevin!
Sep 2 '07 #15
numberwhun
3,503 Expert Mod 2GB
Since it does the same thing as the if...elsif....else statement, I don't think there are really any dis-advantages, unless you are new to Perl and don't know what it is. You may have to field questions from them, but they will (hopefully) quickly catch on.

Regards,

Jeff
Sep 2 '07 #16
KevinADC
4,059 Expert 2GB
What are the disadvantages if any to using the ternery operator?
Seems like a very good way to run comparisons.

Thanks Kevin!

Good question. I am sure there are some disadvantages but I am not sure. As far as good perl coding practices go, the ternary operator should not be used for program flow control, for example:

$foo eq 'bar' ? do_this($foo) : do_that($foo);

But I don't know why that is I have just read it in some books. Probably something to do with using functions that return results in a void context, which is considered poor programming practice. Some people don't like the ternary operator just because it is harder to read for them instead of if/elsif/else blocks.

I tend to use it when the then/else part of the code is simple and short like the example code I posted. If the then/else part gets complicated, using more verbose code would be better.
Sep 7 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Runic911 | last post: by
4 posts views Thread by Lorin Leone | last post: by
32 posts views Thread by ramakrishnadeepak | last post: by
2 posts views Thread by Synapse | last post: by
2 posts views Thread by bigtd08 | last post: by
2 posts views Thread by Kreators | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.