• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

Palindrome

I need to write a program that determines if input is a palindrome or not.  What is the best way to go about this?  I am not using arrays.
0
PMG76
Asked:
PMG76
  • 9
  • 8
  • 4
  • +2
1 Solution
 
rockiroadsCommented:
0
 
PMG76Author Commented:
That code does not run
0
 
jeromeeCommented:
Try this...
$ perl -e'$_=lc($ARGV[0]); s/\s//g; printf("$_ is%s a palindrome\n", $_ eq reverse(split //) ? "" : " NOT")' "a bba"
abba is a palindrome

$ perl -e'$_=lc($ARGV[0]); s/\s//g; printf("$_ is%s a palindrome\n", $_ eq reverse(split //) ? "" : " NOT")' abc
abba is NOT a palindrome

Good luck!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
what's the purpose of the split?
0
 
jeromeeCommented:
Ozo, once again I bow to your knowledge... I've worked with Perl on and off for 15 years and I didn't know that reverse could be used on string. Thanks!


perl -e'$_=lc($ARGV[0]); s/\s//g; printf("$_ is%s a palindrome\n", $_ eq reverse($_) ? "" : " NOT")'
0
 
rockiroadsCommented:
PMG76, what do you mean it didn't work? I just tested it and it worked fine for me. Are you using the right code?
You may of used the initial code from linzhiyi but that is not the one to use, it is the second one from baavgai.

Thanks to strawberries I managed to test and run it and it worked fine.
0
 
rockiroadsCommented:
this perl procedure does the palindrome checking

sub isPalindrome {
      my($line) = shift;

      $line =~ s/\W//g;             
      $line =~ tr/A-Z/a-z/;

      @letters = split //, $line;
      $reverse = join "", reverse @letters;
      
      return ($reverse eq $line);
}


in order to use it you call isPalindrome passing in your string then check the return value. true if it is, false otherwise
eg

      if(isPalindrome("abccba")) {
            print "abccba is a palindrome.\n";
      } else {
            print "abccba is NOT a palindrome.\n";
      }

0
 
rockiroadsCommented:
oh the perl procedure isPalindrome is taken from the link I gave.
0
 
vikaskhoriaCommented:
I think split and joins are not required, this can be done simply like this as well:

If the input string is in $input, then::

$reverse = reverse $input;
if ($input eq $reverse)
{
    print "Palindrome!";
}
else
{
   print "Not a palindrome!";
}

this simple code should work for basic test cases!
0
 
PMG76Author Commented:
for this program I can not use the reverse function or s// with back-referencing replacement.  I just need basic and simple code.
0
 
ozoCommented:
what can be more basic and simple than
$_ eq reverse

(and no back-referencing replacements were used any of the above programs)
0
 
PMG76Author Commented:
I am not allowed to use the reverse function for this program.
0
 
vikaskhoriaCommented:
If reverse function is not allowed then use the following code:

#assuming that you input word is in $input
$input="qwertrewq"; #for test


@chars=split('',$input);

foreach $i (0..$#chars)
{
	if ($chars[$i] ne $chars[$#chars - $i])
	{
		print "Not a palindrome";
		exit;
	}
}
print "Palindrome";

Open in new window

0
 
vikaskhoriaCommented:
If you using user input then you should chomp it, like this:

$input=<STDIN>;
chomp($input);

0
 
rockiroadsCommented:
you need to try clarify what you are limited to at the start. well that said, the only thing I can think of is to iterate thru the string backwards then compare

try calling the procedure below passing in your string

sub isPalindrome {

      my($line) = shift;

      $line =~ s/\W//g;
      $newline = '';

      for ($count=length($line); $count>=0; $count--)
      {
            $newline = $newline . substr ($line,$count,1);
      }

      return ($newline eq $line);
}
0
 
jeromeeCommented:
Also according to wikipedia space are generally accepted:http://en.wikipedia.org/wiki/Palindrome
So you might want to account for that with a:
s/\s//g
or $input =~ s/\s//g

Good luck
0
 
PMG76Author Commented:
That solution is using an array is it not?  I can't use an array or the reverse function in my solution.
0
 
rockiroadsCommented:
what my solution? where do u see an array? I am iterating through a string backwards taking one character at a time to build up a new one
0
 
PMG76Author Commented:
I apologize rockiroads.  I'm just lerarning Perl and it's a bit confusing to me after being used to C++.
0
 
PMG76Author Commented:
rockiroads I get the following error using your code:

Global symbol "$newline" requires explicit package name at ./program4.pl line 36.
Global symbol "$count" requires explicit package name at ./program4.pl line 38.
Global symbol "$count" requires explicit package name at ./program4.pl line 38.
Global symbol "$count" requires explicit package name at ./program4.pl line 38.
Global symbol "$newline" requires explicit package name at ./program4.pl line 40.
Global symbol "$newline" requires explicit package name at ./program4.pl line 40.
Global symbol "$count" requires explicit package name at ./program4.pl line 40.
Global symbol "$newline" requires explicit package name at ./program4.pl line 43.
Execution of ./program4.pl aborted due to compilation errors.
0
 
rockiroadsCommented:
so what compiler and what platform are you using? I am using strawberry on windows

here is my full code

#/usr/bin/perl

test();

sub isPalindrome {

      my($line) = shift;

      $line =~ s/\W//g;
      $newline = '';

      for ($count=length($line); $count>=0; $count--)
      {
            $newline = $newline . substr ($line,$count,1);
      }

      return ($newline eq $line);
}

sub displayTest {
      my($line) = shift;

      if(isPalindrome($line)) {
            print $line . " is a palindrome.\n";
      } else {
            print $line . " is NOT a palindrome.\n";
      }
}

sub test {
      displayTest("aaa");
      displayTest("dog");
      displayTest("mom");
      displayTest("aaamom");
}


0
 
PMG76Author Commented:
I also need to be able for the user to enter their input.  I am using Linux 2.6.28-17-generic #58-Ubuntu SMP Tue Dec 1 21:27:25 UTC 2009 x86_64
0
 
jeromeeCommented:
you must have a 'use strict' somewhere.
either remove it or add 'my' in front of you variables to define them.
0
 
PMG76Author Commented:
Yes, I have to use strict.  this is what I have so far.  I am also not getting any output to tell me if it is a palindrom or not.
#!/usr/bin/perl 

use warnings;
use strict;
print "\n";

print 'Enter in a 7 character phrase: ';

chomp ( my $input = <> );


##test();

sub isPalindrome {

      my($line) = shift;

      $line =~ s/\W//g;
      my $newline = '';

      for (my $count=length($line); $count>=0; $count--)
      {
            $newline = $newline . substr ($line,$count,1);
      }

      return ($newline eq $line);
}

sub displayTest {
      my($line) = shift;

      if(isPalindrome($line)) {
            print $line . " PALINDROME\n";
      } else {
            print $line . " NOT\n";
      }
}

Open in new window

0
 
rockiroadsCommented:
one way is this

      print "Type a word: ";
      $line = <>;

      if(isPalindrome($line)) {
            print $line . " is a palindrome.\n";
      } else {
            print $line . " is NOT a palindrome.\n";
      }

I have not got linux but assuming the first line #!/usr/bin/perl is pointing to the right path, just double check that is right for you. It is ignored in windows.

0
 
rockiroadsCommented:
you are not calling the procedure

try this


#!/usr/bin/perl

use warnings;
use strict;
print "\n";

print 'Enter in a 7 character phrase: ';

chomp ( my $input = <> );


if(isPalindrome($input)) {
      print $input . " PALINDROME\n";
} else {
      print $input . " NOT\n";
}

sub isPalindrome {

      my($line) = shift;

      $line =~ s/\W//g;
      my $newline = '';

      for (my $count=length($line); $count>=0; $count--)
      {
            $newline = $newline . substr ($line,$count,1);
      }

      return ($newline eq $line);
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 8
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now