#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    Palindrome script and dont use Reverse function


    Hi

    I write the script for palindrome script but I am not sure the script is write. Please correct my script.

    #!/usr/bin/perl

    my $i;
    my $str=$ARGV[0];
    my @str=split('',$str);
    while (1){
    if(shift(@str) eq pop(@str)) {

    print "found\n";

    }
    else {
    print "not found\n";

    }
    exit (0);
    }

    =input I passed from the command line is gag or 100 10 100
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    I haven't tried your script, but I do not think it is right.

    You forgot the following pragmas, which should be mandatory for any script having more than two lines.

    Perl Code:
    use strict;
    use warnings;



    Code:
    my $i;
    This seems useless, as $i is never used.

    Perl Code:
    while (1){

    This will never end (infinite loop).

    Perl Code:
    if(shift(@str) eq pop(@str)) {


    Assuming the previous errors were corrected, this would still not work, because it would qualify as a palindrome any word whose first letter and last letter are the same.
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    Originally Posted by lakshmikant
    I write the script for palindrome script but I am not sure the script is write.
    It certainly looks written to me...
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Lightbulb


    Hi Lakshmi, here's one which does not use the reverse function

    perl Code:
     
    # objective: to write a palindrome checker script without using reverse function
    # here's my version, try it out and lemme know
    # i know i have'nt used the strict and warnings
    # but the script is working like a charm
    # pls try it out and lemme know
    # some palindromes for testing:
    # koortsmeetsysteemstrook
    # adam
    # adda
    # hope you have fun ; )
     
    print "Palindrome Checker Script"."\n"; # title of script
    print "*"x25; # just some fancy decor :  )
    print "\n\n";
     
    $palindrome = ""; # this variable will store the palindrome
    print "enter a string: ";
    $str = <STDIN>;
    chomp($str);
    $lenstr = length($str);  # gets the length of string
    for($i=$lenstr;$i>=0;$i--)
    {
    	$palindrome = $palindrome.substr($str,$i,1); # this line generates the palindrome
    }
    print "generated palindrome: ".$palindrome."\n\n";
    if($str eq $palindrome) 
    {
    	print "Result: This is a palindrome!"."\n\n";
    }
    else
    {
    	print "This is not a palindrome..."."\n\n";
    }


IMN logo majestic logo threadwatch logo seochat tools logo