Palindrome Update

w00t! I figured out how to enter in a sentance rather than just words (*) and then to remove the punctuation (**) and put it in lower case (***) and then to unbuffer Perl output (&) and then to print a progress bar through each check (&&). Heheh, its an easy program but its a start.

#!/usr/bin/perl -w
use strict;

# To test for a palindrome 2005 02 28
#-Input—————————————

my $word1 = join( ‘ ‘, @ARGV ); # (*)

if (! $word1)
{
print “\n”;
print “Palindrome 2005\n”;
print “Please enter a word to check: “;

$word1 = ;
chomp ($word1);
}
else
{
print “\n”;
print “Processing on \”$word1\”…\n”;
}

#-Format Inputs——————————-

my $word2 = $word1;

$word1 =~ s/[,\.!\?\s@#\$\%^\*-=;'":]//g; # (**)
$word1 = lc($word1); # (***)
my @array_word1 = split (//, $word1);
my $array_word1_length = scalar @array_word1;

#-Test—————————————-

my $palindrome = 1;
my $i;
my $x = $array_word1_length – 1;
$| = 1; # (&) 1=on, 0=off

for($i = 0; $i <= ($x - $i); $i++)
{
print "."; # (&&)
sleep 1;
if ($array_word1[$i] ne $array_word1[($x - $i)])
{
$palindrome = 0;
last;
}
}

#-Result————————————–

my $not;

if ($palindrome == 1)
{
$not = “”;
}
else
{
$not = ” not”;
}

print “\n”;
print “The word or phrase \”$word2\” is$not a palindrome!\n”;
print “\n”;

#=End=========================================

This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.