Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I really need help and I feel stupid on not knowing how to make this but I am completely stumped as to what to do. I need to make a palindrome program that reads 7 characters from a user and tests to see if it is a palindrome or not. But I cannot use a reverse function, and everywhere I look people use the reverse function, can anyone point me in the right direction without actually giving me the answer? This is what I have so far
#!/usr/bin/perl -w use strict; use warnings; my ($a, $b, $result); print "Enter a item that is 7 characters\n"; chomp ($a = <>); chomp ($b = <>); if ($a eq $b && $b eq $a) { print "THIS IS A PALINDROME!!! \n"; } else { print "THIS IS NOT A PALINDROME \n"; }

Replies are listed 'Best First'.
Re: Palindrome Program without Reverse function
by Ratazong (Monsignor) on Oct 30, 2011 at 19:09 UTC

    Hi

    The examples you find look probably like

    my $back = reverse $forw; if ($back eq $forw) { # it is a palindrome
    In order to avoid the reverse function you could try to code it yourself: loop through the string $forw character by character, and construct $back by appending the characters at the correct place.

    Hope that idea helps you! Rata
Re: Palindrome Program without Reverse function
by moritz (Cardinal) on Oct 30, 2011 at 20:00 UTC

    First of all you should only read one string from <>.

    If you were given a long word on paper, and were to check if it is a palindrome, how would you do it? Just write a program that takes the same approach as you would.

Re: Palindrome Program without Reverse function
by ikegami (Patriarch) on Oct 30, 2011 at 21:54 UTC

    It's a palindrome if

    str[0] eq str[str_length-1] and str[1] eq str[str_length-2] and str[2] eq str[str_length-3] and ...

    Or, it's not a palindrome if

    str[0] ne str[str_length-1] or str[1] ne str[str_length-2] or str[2] ne str[str_length-3] or ...
Re: Palindrome Program without Reverse function
by ww (Archbishop) on Oct 31, 2011 at 02:39 UTC
    perldoc -f shift and following this bit of advice therein...
    See also "unshift", "push", and "pop". "shift" and "unshift" do the same thing to the left end of an array that "pop" and "push" do to the right end.

    and understanding how to split on character boundaries...
    and you have it knocked!.

Re: Palindrome Program without Reverse function
by Anonymous Monk on Oct 31, 2011 at 04:37 UTC
    my $palindrome_re = qr{ \b (?<pal> # A palindrome is a sandwich... (?<char>\w) # starting with white bread... (?: # filled with either... \w? # peanut butter (maybe)... | # or... (?&pal) # another sandwich... ) \k<char> # topped off with white bread! ) \b }x; @ARGV = 'words'; /$palindrome_re/ and print while <>;

      Clever, but slow:

      sub isPalindrome{ substr( $_[0], $_, 1 ) eq substr( $_[0], -($_+1), 1 ) or return for 0 .. length( $_[0] ) >> 1; 1; }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      ...Liked the regex; not so much, lines 15 and 16...
      so tried 5.012 which didn't like 16 either.

      So left the regex alone and re-wrote Lines 15-16 (much more verbosely) to work nicely with 5.12:

      #!/usr/bin/perl use Modern::Perl; # 934820 my $palindrome_re = qr{ \b (?<pal> # A palindrome is a sandwich... (?<char>\w) # starting with white bread... (?: # filled with either... \w? # peanut butter (maybe)... | # or... (?&pal) # another sandwich... ) \k<char> # topped off with white bread! ) \b }ix; my @Rev_arr = qw /Research tattarrattat Kanakanak Qaanaaq Foobarblivit +z/; for my $word(@Rev_arr) { if ($word =~ /$palindrome_re/) { say $word; } else { say "$word is NOT a palindrome"; } }
      Execution:
      C:\>934820.pl Research is NOT a palindrome tattarrattat Kanakanak Qaanaaq Foobarblivitz is NOT a palindrome C:\>
Re: Palindrome Program without Reverse function
by JavaFan (Canon) on Oct 31, 2011 at 11:03 UTC
    A word is a palindrome if it's either the empty string, a string of one character, or if it's a palindrome proceeded and succeeded by the same character. So, I present you the following, untested, subroutine:
    my is_palindrome {length($_[0])<2||substr($_[0],0,1) eq substr($_[0],- +1,1)&&is_palindrome(substr($_[0],1,-1);}
      Tested, fixed: sub is_palindrome {length($_[0])<2||substr($_[0],0,1) eq substr($_[0],-1,1)&&is_palindrome(substr($_[0],1,-1))}
Re: Palindrome Program without Reverse function
by kranthi_votary (Initiate) on Jul 16, 2015 at 04:41 UTC

    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!!!"; }
      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";
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ