6 Replies - 14119 Views - Last Post: 14 May 2008 - 03:08 AM Rate Topic: -----

#1 dummyinphp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 08

Perfect Palindrome?

Post icon  Posted 26 April 2008 - 11:44 AM

Does anyone know how to write a script in Perfect Palindrome?
Is This A Good Question/Topic? 0
  • +

Replies To: Perfect Palindrome?

#2 herefishyfishy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 60
  • Joined: 01-May 08

Re: Perfect Palindrome?

Posted 01 May 2008 - 06:01 PM

I don't really understand your question. "Perfect Palindrome" is not a programming term, I don't think... but if you're asking how to write a script that will check whether a string is a palindrome, you need to use substr(), strlen() and iteration.

EDIT: Never mind, you could also use strtolower(), explode(), implode(), and strrev(), which would be easier.

This post has been edited by herefishyfishy: 01 May 2008 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5060
  • View blog
  • Posts: 26,429
  • Joined: 10-May 07

Re: Perfect Palindrome?

Posted 01 May 2008 - 06:05 PM

My understanding is that a word either is a Palindrome, or it is not. I'm not sure what would make it stand out enough to be considered, "Perfect"

To check it, you would simply place the letters of the word into an array, & run a loop through each letter checking it against the opposite position.

So if the word was 3 characters long,
if($array[0]==$array[2]) // Palindrome
Was This Post Helpful? 0
  • +
  • -

#4 Eighty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 16-April 08

Re: Perfect Palindrome?

Posted 02 May 2008 - 06:42 PM

A perfect palindromic script:
<?php?>

Was This Post Helpful? 0
  • +
  • -

#5 chrisman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 22-March 08

Re: Perfect Palindrome?

Posted 03 May 2008 - 06:21 PM

I don't think it is possible for a code to be useful when written in a perfect palidrome. And by perfect palindrome, I assume you mean a palidrome where not only the letters match up on either side, but spaces do too.

<? echo "ohce" ?>
// Works, but quotes don't match.
// Semi-perfect palindrome?


Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: Perfect Palindrome?

Posted 04 May 2008 - 09:32 AM

Well it is said that a perfect palindrome is one which matches spaces as well as the actual characters. Even though I don't see a formal definition of this phrase anywhere.

Having said that, below is an example of a recursive palindrome function which should work. It is not case sensitive but if you want it to be, you could just make it a triple equal sign instead of a double.

	// Tests for a perfect palindrome which is a palindrome which matches spaces as well as lettering
	// forwards and backwards.

	function perfectPalindrome($string) {
		// Base case
		if ((strlen($string) == 1) || (strlen($string) == 0)) {
			return true;
		}
		else {
			// Compare first letter and last letter
			// Not case sensitive but could be if you make it === in the line below
			if (substr($string,0,1) == substr($string,(strlen($string) - 1),1)) {
				return perfectPalindrome(substr($string,1,strlen($string) -2));
			}
			else { return false; }
		}
	}



Feel free to analyze the code and run your tests on it. Enjoy!

"At DIC we be kickassssakcik code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#7 therockery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-May 08

Re: Perfect Palindrome?

Posted 14 May 2008 - 03:08 AM

Hi. I have kinda the same question. also have to write a function for checking if a word/phrase is a palindrome. But is has not to be perfect, so don't need to match on spaces. That is why before calling the function i have written this code

$palindrome=trim($palindrome);

but how do i change now your code Martyr 2 in the way that it doesn't match on spaces anymore??? sorry for the stupid question, but am a total looser in writing functions :(

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1