Check the code I have tried
My question is little bit different than others. I want to make an efficient code for palindrome.So I have added some constrains.Constrains:No storing the string in any extra variable.Use only one loop.I have tried it in php,but it is not rendering the output. I have used the same logic in the image.I am new to php.I need some help to check my code is right,if wrong what changes can be made?No matter whether it is php or any other oops language,I just need to check whether my logic is right.
PHP code:
class user{
public function __construct(){
$this->palindrome();
}
public function palindrome(){
str ="abba";
i=0;
while(str[i] == str[strlen(str-1)-i])
{
i++;
}
if(i > strlen(str)/2)
{
return 0;
}
}
}
$obj = new user;
Keeping your logic, corrected the syntax errors.
<?php
class user {
public function __construct() {
if ($this->palindrome()) {
echo 'Yes, Palindrome';
} else {
echo 'Not a palindrome';
}
}
public function palindrome() {
//Need $ symbol
$str = "abba";
$i = 0;
// \/
while ($str[$i] == $str[strlen($str) - ($i + 1)]) {
$i++;
if ($i > strlen($str) / 2) {
return 1;
}
}
return 0;
}
}
;
$obj = new user();