palindrome problem

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

Page 1 of 1

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

#1 linzhiyi  Icon User is offline

  • New D.I.C Head

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

palindrome problem

Post icon  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{
 
	# 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: 08 December 2007 - 08:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: palindrome problem

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • 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{
		# read the input
		print "Type a word or phrase: ";
		$line = <>;
		displayPalindromeResults($line);
	} while($line);
}

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


Was This Post Helpful? 0
  • +
  • -

#3 linzhiyi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1