Welcome to Dream.In.Code
Become an Expert!

Join 150,224 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,253 people online right now. Registration is fast and FREE... Join Now!




palindrome problem

 
Reply to this topicStart new topic

palindrome problem, my code can judge if a string is palindrome one by one. how to make it

linzhiyi
8 Dec, 2007 - 06:30 PM
Post #1

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
my code can judge if a string is palindrome one by one. then how to make it can input more than one and judge them together. for example, input five together, then it will tell which is palindrome, which is not.
CODE

do{

    # read the input
    print "Type a word or phrase: ";
    $line = <>;
    
    # strip out stuff that would disturb the palindrome comparison
    # and convert to lowercase...
    $line =~ s/\W//g; # removes space and nonalphanumerics        
    $line =~ tr/A-Z/a-z/;  # converts to lowercase. Better: lc($line) !

    # get the list of letter and reverse it
    @letters = split //, $line;
    $reverse = join "", reverse @letters;
    
    # a palindrome is equal to its reverse or a one letter word.
    if(@letters == 0){
    print "End of session\n";
    }
    elsif(@letters == 1){
    print "One letter palindrome, trivial!\n";
    }
    elsif($reverse eq $line){
    print "This is a palindrome.\n";
    }
    else{
    print "Not a palindrome.\n";
    }
}while($line);


This post has been edited by linzhiyi: 8 Dec, 2007 - 07:06 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Palindrome Problem
8 Dec, 2007 - 07:30 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,285



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Sub procedures are your friends! If the logic is in a single command, it should be easy enough.

CODE

#!/usr/bin/perl

test();

sub isPalindrome {
    my($line) = shift;

    # strip out stuff that would disturb the palindrome comparison
    # and convert to lowercase...
    $line =~ s/\W//g; # removes space and nonalphanumerics        
    $line =~ tr/A-Z/a-z/;  # converts to lowercase. Better: lc($line) !

    # get the list of letter and reverse it
    @letters = split //, $line;
    $reverse = join "", reverse @letters;
    
    return ($reverse eq $line);
}

sub displayPalindromeResults {
    my($line) = shift;
    
    print $line;
    
    my($lineLen) = length($line);
    
    if ($lineLen==0) {
        print "End of session\n";
    } elsif($lineLen==1) {
        print " is a one letter palindrome, trivial!\n";
    } elsif(isPalindrome($line)) {
        print " is a palindrome.\n";
    } else {
        print " in NOT a palindrome.\n";
    }
}

sub userInput {
    do{
        # read the input
        print "Type a word or phrase: ";
        $line = <>;
        displayPalindromeResults($line);
    } while($line);
}

sub test {
    displayPalindromeResults("a");
    displayPalindromeResults("foo");
    displayPalindromeResults("mom");
}


User is online!Profile CardPM
+Quote Post

linzhiyi
RE: Palindrome Problem
9 Dec, 2007 - 09:21 PM
Post #3

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 20


My Contributions
thank you for your help, but use the changed one, all the input are inside the perl script, how to make they can input in the commend window by the users.
and use perl to judge whether they are palindrome.
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Palindrome Problem
10 Dec, 2007 - 11:17 AM
Post #4

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
You should be doing your own school/course work linzhiyi
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:58AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month