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