4

this is php code using function argument to check string is palindrome or not..help me out in telling me step by step ..whats the process is happening...iam unable to understand

function Palindrome($string) {

    if ((strlen($string) == 1) || (strlen($string) == 0)) {
        echo " STRING IS PALINDROME";
    }

    else {

        if (substr($string,0,1) == substr($string,(strlen($string) - 1),1)) {
            return Palindrome(substr($string,1,strlen($string) -2));
        }
        else { echo " STRING IS NOT A PALINDROME"; }
    }
}


Palindrome("121");
andrewsi
  • 10,807
  • 132
  • 35
  • 51
user2334992
  • 49
  • 1
  • 2
  • 2
    hey, is this maybe some kind of homework? 2 distinct users with the same question... http://stackoverflow.com/questions/17237021/function-based-on-palindrome-using-php-code – STT LCU Jun 21 '13 at 13:59

5 Answers5

3

This function takes a string argument as input

The first thing it does is checks the recursive base case. We'll get back to that

If the base case is not satisfied, it then checks to see if the first character matches the last character using this code:

if (substr($string,0,1) == substr($string,(strlen($string) - 1),1))

If that does match, then the function recursively calls itself again but this time with the first and last character removed, this is done with this line

return Palindrome(substr($string,1,strlen($string) -2));

If ever the first character does not match the last character, the function automatically outputs to html "STRING IS NOT A PALINDROME via echo

Now back to the base case I mentioned before, if the function successfully matches and removes the first and last character until there are one or no characters left, then the string has been confirmed to be a palindrome and it echos that string.

If you need help with recursion let me know, I'll post a tutorial link

Stephan
  • 16,509
  • 7
  • 35
  • 61
1

It is using recursion.

The program works this way. If the word has no letters or has one letter it is a palindrome. 'a' is a palindrome and so is ''

So your program will check if the first letter matches the last letter in the given word. If not its not a palindrome If it does, it will remove the last letter and first letter and last letter and check again

So when you say 121 1. It will check 1 and 1, which match. So it will call the same function with 2 2. Then it will remove 1 and 1 3. It will check 2 4. Since the 2 is one character it is a palindrome

If u ask for abba

  1. It will check for a and a, which match. So it will call the same function with bb
  2. It will check for b and b

If u ask for phpcodephp

  1. It will match p and p. So it will call the same function with hpcodeph
  2. It will match h and h. So it will call the same function with pcodep
  3. it will match p and p. So it will call the same function with code
  4. It will match c and e which do not match. So it is not a palindrome
Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
1

It is using recursive function to check the string is palindrome or not. Firstly it is checking whether length of string in 1 or 0 by strlen() function. If yes then directly return "yes", else it will check first letter i.e substr($string,0,1) with last letter i.e. substr($string,(strlen($string) - 1),1). If it is true then it will recursively check with the substring with excluding the first letter and the last letter i.e. Palindrome(substr($string,1,strlen($string) -2));.

Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51
0

use

function Palindrome($string) {
    return strrev($string) === $string;
}

var_dump(Palindrome("121"));

your function checks the length, if its 0 or 1 it has to be a palindrome.

It then compares the first letter to the last. it they are the same, Take then both off an repeat.

so a call of 121 goes

121 is not equal to 0 or 1 char long,

check first is equal to last. 1 = 1

remove both 1's leaving 2

restart with 2 as the argument.

2 is equal to 0 or 1 char long,

return String is Palindrome.

the function ive posted is a much easier way to check

exussum
  • 18,275
  • 8
  • 32
  • 65
0

This algorithm checks, if the first and the last letter of a string is equal. If it's the case, the algorithm crops the first and the last letter of the string, and calls itself recursivly. And this, till the string is empty (or only one letter left) For example the string "blaalb" The algorithm is called using Palindrome("blaalb"). As you can see, the first and the last letter is the same ("b"), so those letters are cropped. (if not, the string would not be a palindrome and "STRING IS NOT A PALINDROME" will be returned) the algorithm now calls itself with Palindrome("laal"). Again, "l" gets cropped, and then "a" not the string is emptyand "STRING IS A PALINDROME" gets returned. Keep in mind, that for example "blaxalb" is also a palindrome, and therefore the algorithm checks if the string length (strlen) is 1 or 0 to know it's finished