Thursday, 27 March 2014

Program to find a string is Palindrome or Not in Php

<?php
$word = "Malayalam";  // declaring a varibale
echo "String: " . $word . "<br>";
$reverse = strrev($word); // reversing word
if ($word == $reverse) // comparing if original word is same as the reverse of the same word
    echo 'Output: The string is a palindrome';
else
    echo 'Output: This is not a palindrome';
?>

Find the string palindrome or not in PHP without using strrev() function.

<?php
$mystring = "madam"; // set the string
echo "String: " . $mystring;
$myArray = array(); // php array
$myArray = str_split($mystring); // split the array
$len = sizeof($myArray); // get the size of array
$newString = "";

for ($i = $len; $i >= 0; $i--) {
    $newString.=$myArray[$i];
}
echo "<br>";
if ($mystring == $newString) {
    echo "Output: " . $mystring . " is a palindrome";
} else {
    echo "Output: " . $mystring . " is not a palindrome";
}
?>

1 comment:

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...