<?php //Create a function IsPalindrum($str) to see if it's true or false //Use arrays!!! //String is a arrays of characters //i.e racecar // r = $i[0] etc. function IsPalindrome($str) { $str = str_replace(' ', '', $str); if ($str == strrev($str)) { echo "{$str} is a palindrome"; } else { echo "{$str} is not a palindrome"; } } $str = 'race car'; IsPalindrome($str);
<?php function IsPalindrome($str) { $str = str_replace(' ', '', $str); $str = str_replace('.', '', $str); $str = strtolower($str); $str_array = str_split($str); $reverse = ''; for ($i = count($str_array) - 1; $i >= 0; $i--) { $reverse = $reverse . $str_array[$i]; } if ($str == $reverse) { echo "{$str} is a palindrome"; } else { echo "{$str} is not a palindrome"; } } $str = 'Race car'; IsPalindrome($str); echo "<br/>"; IsPalindrome('Kayak'); echo "<br/>"; $string = 'Too hot to hoot.'; IsPalindrome($string); //Question 1: Special character replacement function? //Question 2: $str is showing replaced version. How to show the original value? ---- by giving new var
* a given sequence of 10 positive integers. */ function IsPalindrome($K) { $str = intval($K); $arr2 = ''; $arr1 = str_split($str); // print_r($arr1); //echo '<br/>'.$len ; foreach ($arr1 as $elements) { $arr2 = $elements . $arr2; } $arr2 = str_split($arr2); //print_r() //print_r($arr2); if ($arr2 == $arr1) { $result = 'true'; } else { $result = 'false'; } return $result; } $var = 12321; $response = IsPalindrome($var); echo 'is ' . $var . ' a palindrome ? ' . $response . '<br/>'; $var1 = 1234321; $response = IsPalindrome($var1); echo 'is ' . $var1 . ' a palindrome ? ' . $response . '<br/>'; $var2 = 123211; $response = IsPalindrome($var2); echo 'is ' . $var2 . ' a palindrome ? ' . $response . '<br/>';