Solved

Palindrome

Posted on 2010-09-05
26
675 Views
Last Modified: 2012-05-10
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
Comment
Question by:PMG76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 4
  • +2
26 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33606890
0
 

Author Comment

by:PMG76
ID: 33606955
That code does not run
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33607699
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!

 
LVL 84

Expert Comment

by:ozo
ID: 33608004
what's the purpose of the split?
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33608327
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33609073
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33609119
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33609121
oh the perl procedure isPalindrome is taken from the link I gave.
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33609258
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
 

Author Comment

by:PMG76
ID: 33611145
for this program I can not use the reverse function or s// with back-referencing replacement.  I just need basic and simple code.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33611183
what can be more basic and simple than
$_ eq reverse

(and no back-referencing replacements were used any of the above programs)
0
 

Author Comment

by:PMG76
ID: 33611433
I am not allowed to use the reverse function for this program.
0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33611801
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
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33611846
If you using user input then you should chomp it, like this:

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

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33611913
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
 
LVL 10

Expert Comment

by:jeromee
ID: 33611915
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
 

Author Comment

by:PMG76
ID: 33611965
That solution is using an array is it not?  I can't use an array or the reverse function in my solution.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33611984
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
 

Author Comment

by:PMG76
ID: 33612005
I apologize rockiroads.  I'm just lerarning Perl and it's a bit confusing to me after being used to C++.
0
 

Author Comment

by:PMG76
ID: 33612047
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33612110
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
 

Author Comment

by:PMG76
ID: 33612128
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
 
LVL 10

Expert Comment

by:jeromee
ID: 33612188
you must have a 'use strict' somewhere.
either remove it or add 'my' in front of you variables to define them.
0
 

Author Comment

by:PMG76
ID: 33612204
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
 
LVL 65

Expert Comment

by:rockiroads
ID: 33612211
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
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 33612228
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question