Solved

Palindrome

Posted on 2010-09-05
26
635 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
  • 9
  • 8
  • 4
  • +2
26 Comments
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
0
 

Author Comment

by:PMG76
Comment Utility
That code does not run
0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
what's the purpose of the split?
0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oh the perl procedure isPalindrome is taken from the link I gave.
0
 
LVL 5

Expert Comment

by:vikaskhoria
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I am not allowed to use the reverse function for this program.
0
 
LVL 5

Expert Comment

by:vikaskhoria
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:vikaskhoria
Comment Utility
If you using user input then you should chomp it, like this:

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

0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now