there is a String 'S', program is to find and print whether this string is a palindrome or not. If yes, print "YES", else print "NO"
for some palindrome words it is working like civic and for some it is not working code:-
<?php
$str = "Civic";
$count = ((strlen($str)+1 ) /2);
$q = "y";
for($i = 0; $i < $count; $i++)
{
$x = 1;
$z = $str[(strlen($str-$x))];
$x = $x+1;
if($str[$i] == $z ) {
$x = $x++;
$q = "y";
} else {
$q = "n";
}
}
if($q == "n") {
echo "No";
} else {
echo "Yes";
}
?>
Please give me some suggestions about my code, Thanks
How about a different approach:
if($str == strrev($str)) {
echo "Yes";
} else {
echo "No";
}
Or shorter:
echo ($str == strrev($str)) ? "Yes" : "No";
But maybe the problem is case sensitivity?
echo (strtolower($str) == strrev(strtolower($str))) ? "Yes" : "No";