Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Created July 9, 2018 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosfreitas/94fd25033f89520dbfa767876f3843af to your computer and use it in GitHub Desktop.
Save marcosfreitas/94fd25033f89520dbfa767876f3843af to your computer and use it in GitHub Desktop.
#training Is A Palíndrome word
<?php
# check for palindrome words
# if the word have almost 1 letter to be changed to be a plindrome, it will be a palíndrome.
function isAlmostPalindrome($word) {
$count_diference = 0;
$word_reverse = strrev($word);
$word_as_array = str_split($word);
foreach(str_split($word_reverse) as $key => $letter) {
if ($letter !== $word_as_array[$key]) {
$count_diference++;
}
if ($count_diference > 2) {
return " it isn't a palindrome";
} elseif($key === sizeof(str_split($word_reverse))-1) {
return " it is a palindrome";
}
}
return " it " . ( ($word_reverse === $word) ? ' is ' : " isn't" ) . ' a palindrome';
}
echo isAlmostPalindrome('abccba') .'<br>';
echo isAlmostPalindrome('abccbx') .'<br>';
echo isAlmostPalindrome('abcckx') .'<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment