# palindrome problem

Page 1 of 1

## 3 Replies - 5907 Views - Last Post: 10 December 2007 - 12:17 PM

### #1 linzhiyi

Reputation: 0
• Posts: 23
• Joined: 08-December 07

# palindrome problem

Posted 08 December 2007 - 07:30 PM

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.
```do{

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: 08 December 2007 - 08:06 PM

Is This A Good Question/Topic? 0

## Replies To: palindrome problem

### #2 baavgai

• Dreaming Coder

Reputation: 7164
• Posts: 14,932
• Joined: 16-October 07

## Re: palindrome problem

Posted 08 December 2007 - 08:30 PM

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

```#!/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{
print "Type a word or phrase: ";
\$line = <>;
displayPalindromeResults(\$line);
} while(\$line);
}

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

```

### #3 linzhiyi

Reputation: 0
• Posts: 23
• Joined: 08-December 07

## Re: palindrome problem

Posted 09 December 2007 - 10:21 PM

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.

• D.I.C Regular

Reputation: 27
• Posts: 401
• Joined: 23-January 07

## Re: palindrome problem

Posted 10 December 2007 - 12:17 PM

You should be doing your own school/course work linzhiyi