in reply to Palindrome Program without Reverse function

Try this!!!!

#!\\C:\\Perl\\bin\\perl -w my $name=<STDIN>; chomp($name); @arr=split(//,$name); $count=@arr; $num=0; #print $count; for ($i=0;$i<$count;$i++){ if ($arr[$i] eq $arr[$count-1]){ #print "$arr[$i] ===== $arr[$count]\n"; $count--; }else{ $num++; last; } } if ($num == 0){ print "Word is polindrome!!!"; }else{ print "Word is not a Polindrome!!!"; }

Replies are listed 'Best First'.
Re^2: Palindrome Program without Reverse function
by choroba (Cardinal) on Jul 16, 2015 at 13:27 UTC
    You replied to a 4 years old thread. Moreover, you check half the pairs pointlessly - there's no need to check $arr[$#arr] versus $arr[0], when you've already checked $arr[0] versus $arr[$#arr]. Here's how I'd code your idea:
    #!/usr/bin/perl use strict; use warnings; chomp(my $name = <>); my @arr = split //, $name; my $is_palindrome = 1; for my $i (0 .. $#arr / 2) { # warn "$arr[$i] eq $arr[ -$i - 1 ]"; if ($arr[$i] ne $arr[ -$i - 1 ]) { undef $is_palindrome; last } } print 'Word is', $is_palindrome ? q() : ' not', " a palindrome!!!\n";
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ