Checking the state of a palindrome using function arguments

this is php code using a function argument to check the string - is it a palindrome or not .. help me in telling me step by step .. that 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"); 
+4
source share
5 answers

This function takes a string argument as input

The first thing he does is check the recursive base argument. We will return to this

If the base case fails, it then checks to see if the first character matches the last character with this code:

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

If this matches, then the function recursively calls itself again, but this time with the removal of the first and last characters this is done using this line

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

If 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, which I mentioned earlier, if the function successfully matches and deletes the first and last characters until one or more characters remain, then the line was confirmed as a palindrome, and this echo points to this line.

If you need help with recursion, let me know, I will send a link for the tutorial

+3
source

Recursion is used.

The program works this way. If the word has no letters or has one letter, it is a palindrome. "a" is a palindrome as well as "

So, your program will check if the first letter matches the last letter in the given word. If not his palindrome. If this happens, he will delete the last letter, the first letter and the last letter and check again

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

If you ask abba

  • He will check a and a, which match. Therefore it will call the same function with bb
  • Will be checked on b and b

If you request phpcodephp

  • It will match p and p. Therefore, it will call the same function with hpcodeph
  • It will match h and h. This way it will call the same function with pcodep
  • it will match p and p. Therefore, it will call the same function with the code
  • It will match c and e which do not match. So this is not a palindrome
+1
source

Using a recursive function to check a string is a palindrome or not. The first is checking the string length to 1 or 0 using the strlen () function. If yes, then directly return β€œyes”, otherwise it will check the first letter ie substr ($ string, 0,1) with the last letter ie Substr ($ string, (strlen ($ string) - 1), 1). If this is true, then it will recursively check the substring, excluding the first letter and last letter, that is, Palindrome (substr ($ string, 1, strlen ($ string) -2)) ;.

+1
source

use

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

your function checks the length if its 0 or 1 it should be a palindrome.

He then compares the first letter with the last. they are one and the same, take both repetitions.

so call 121 goes

121 is not 0 or 1 char is long,

check first equals the last. 1 = 1

delete both remaining 2

restart with argument 2.

2 is 0 or 1 char long,

return String is a palindrome.

ive function published is much easier to check

0
source

This algorithm checks if the first and last letter of a string is equal. If so, the algorithm processes the first and last letter of the string and calls itself recursive. And this, until the line is empty (or only one letter remains) For example, the string "blaalb" The algorithm is called using Palindrome ("blaalb"). As you can see, the first and last letter are the same ("b"), so these letters are truncated. (if not, the string will not be a palindrome and "STRING IS NOT A PALINDROME" will be returned), now the algorithm now calls itself Palindrome ("laal"). Again, β€œl” is truncated, and then β€œa” is not a line empty and β€œSTRING IS PALINDROME” is returned. Keep in mind that, for example, "blaxalb" is also a palindrome, and therefore the algorithm checks to see if the string length (strlen) is 1 or 0 to know that it is complete.

0
source

Source: https://habr.com/ru/post/1487475/


All Articles