A string is a combination of characters. If a string has to be considered as palindrome if the reverse of the string will be the same as the original string. For example, if radar is the entered string and if we check the reverse it will also be radar so it is a palindrome string and in another case, if the algorithm is the string and if we check the reverse it will be mhtirogla and it is not same as the original string so it is not a palindrome string.
In this program to check the string is palindrome or not, we are using a user-defined function. While checking for a palindrome sting the program considers lowercase and upper case letters as different to avoid this we are converting the string to the lower case using the built-in function strtolower()
before finding the reverse and checking the reverse with the original string. In here we first accept the input from the user and store it to the variable char and call the user-defined function
checkPalindrome() with argument char. And in the function checkPalindrome() we first define a null string variable str which will be used to store the reverse of the string. Then we convert the value of the variable char to lower case and store it to the variable s and then we find the length of the char and store it to the variable len. Then we perform for loop
to reverse the string. In for loop first, we assign 'len-1' to the variable i and perform the loop until the condition 'i >= 0' becomes false and also decrement the value of the variable i by 1 in every iteration and in the block of the loop we perform the concatenation operation 'str. s[i]' and assign it to the variable str. And after the completion of the loop, we check that the value of variables s is the same as str if it is true then print the string as a palindrome string otherwise print the string as not a palindrome string.
Step 1: Accept the string input from the user and store it in the variable char
Step 2: Call the function checkPalindrome() with the value of the variable char as the argument
Step 1: Assign a blank string variable str
Step 2: Convert the value passed through the function argument to lower case using the built-in function strtolower() and assign it to the variable s
Step 3: Find the length of the variable char and assign it to the variable len
Step 4: Assign the computed value of 'len - 1' into the variable i and perform the following sub-step until the condition 'i >=0' becomes false and decrement the value of variable i by 1 in every iteration
(i) Assign the value of operation 'str . s[i]' to the variable str
Step 5: Check the condition 's == str' if the condition if true print the entered string as palindrome otherwise print it as not a palindrome string
<?php
function checkPalindrome($char)
{
$str = "";
$s = strtolower($char);
$len = strlen($char);
for ($i = ($len - 1); $i >= 0; $i--) {
$str = $str . $s[$i]; // concatenating the character of each index with the value of variable str
}
if ($s == $str) {
echo "$char is a palindrome string";
} else {
echo "$char is not a palindrome string";
}
}
$char = readline("Enter the string: ");
checkPalindrome($char);
?>
Example 1: Enter the string: tenet tenet is a palindrome string Example 2: Enter the string: website website is not a palindrome string