473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 17172
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,509 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,509 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,509 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

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

Similar topics

24
by: Runic911 | last post by:
Does anyone know how i can fix my Palindrome program? s = raw_input('Enter a String: ') punctuation = '%$!*.,-:? ;()\'\"\\' i = 0 h = 0 t = 0 p = '' z = 0 while s!= ' ':
4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
32
by: ramakrishnadeepak | last post by:
HI Everybody, I 've to submit a program on c.Can any one help me plz.........The problem is like this:: Write a program which computes the largest palindrome substring of a string. Input:...
2
by: Synapse | last post by:
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it. If I enter 33633, 11211, 45554, it will...
1
by: 1051109210 | last post by:
class MyStack{ public: MyStack(int sz=10); ~MyStack(); char pop(); void push(char ch); bool isEmpty(); bool isFull(); private: char *value;
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
2
by: Kreators | last post by:
Hi everyone i'm newbie here in bytes and a newbie in programing i need your help! can you correct my program when my program compile its say Process completed. but when i put a Word and Run my...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.