Friday, May 22, 2015

Palindrome solution type2 using array_reverse

In this tutorial i created an function that will tell if a given word is palindrome or not this is another solution to palindrome using array_reverse
 
 palindrome('axa');
 function palindrome($input){ 
    $split = str_split($input); //make the input array
    $reverse = array_reverse($split); // reverse the array
    foreach($reverse as $char){
      $newChar .= $char; //append the letters
    } 
    if($input === $newChar){
      echo "Palindrome";
    }else{
      echo "Not Palindrome";
    }
 }
the result will look like this
Palindrome
note that "axa" is the word that i want to check if it is palindrome or not, you can change it to the one you desired to check.

No comments:

Post a Comment