Last active
July 11, 2018 13:40
-
-
Save HoraceShmorace/05cb97fb9cffaed807fc8c937aa02508 to your computer and use it in GitHub Desktop.
Palindrome checker class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy | |
class Palindrome { | |
public static function isPalindrome($word) { | |
$word = str_split(strtolower($word)); | |
for($i = 0; $i < ceil(count($word)/2); $i++) { | |
$leftChar = $word[$i]; | |
$rightChar = $word[count($word)-$i-1]; | |
if($leftChar !== $rightChar) { | |
return FALSE; | |
} | |
} | |
return TRUE; | |
} | |
} | |
echo Palindrome::isPalindrome('Deleveled'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy | |
class Palindrome | |
{ | |
public static function isPalindrome($word) { | |
$word = strtolower($word); | |
return $word == strrev($word); | |
} | |
} | |
echo Palindrome::isPalindrome('Deleveled'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy | |
class Palindrome { | |
public static function isPalindrome($word) { | |
$word = strtolower($word); | |
for($i = 0; $i < ceil(strlen($word)/2); $i++) { | |
$leftChar = substr($word, $i, 1); | |
$rightChar = substr($word, -($i+1), 1); | |
if($leftChar !== $rightChar) { | |
return FALSE; | |
} | |
} | |
return TRUE; | |
} | |
} | |
echo Palindrome::isPalindrome('Deleveled'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment