-1

I have created for code to check if its palindrome. Now if the string is not a palindrome I want it to be reversed.Can it be done using one conditional loop? My php code:

class user{

public function __construct() {
    if ($this->String_Rev()) {
        echo 'Yes, palindrome';

    } else {
        echo 'not palindrome';
    }
}

public function String_Rev() {

   $str = "abba";

    $i = 0;

    while ($str[$i] == $str[strlen($str) - ($i + 1)]) {//Incrementing and decrementing the values in the string at the same time/
        $i++;


        if ($i > strlen($str)/ 2) {//If the  i goes ahead of half of its string then return true and stop its execution.
            return 1;
        }
    }
    return 0;
}

 }



 $obj = new user();
tereško
  • 58,060
  • 25
  • 98
  • 150
Ked
  • 11
  • 7
  • What do you mean with "no predefined function"? As in that you want to manually reverse the string? Because this could be a whole lot easier with just [strrev()](http://php.net/manual/en/function.strrev.php) – icecub Nov 26 '17 at 03:35
  • Why not just reverse the string anyway? If it's a palindrome, reversing it has no effect. You can't check the string AND reverse it in a single loop. – Jim Mischel Nov 26 '17 at 03:45
  • @JimMischel Well I guess technically you could compare the first and last character followed by the second and the one before the last etc in a single loop and determine if it's a palindrome that way. Terrible way to do it though. – icecub Nov 26 '17 at 03:52
  • @icecub The algorithm you describe is the one that he posted. That's the fastest algorithm I know of. – Jim Mischel Nov 26 '17 at 03:55
  • Possible duplicate of [Reverse a string with php](https://stackoverflow.com/questions/11100634/reverse-a-string-with-php) – yivi Nov 26 '17 at 09:13

1 Answers1

0

Strings in PHP are not arrays, but you can select the character index of a string just like an array key, which makes the following possible:

<?php
$string = 'foobar';

$string_array = array();

//make an actual array of the characters first
//if you want this to be an object, do this part in your constructor,
//and assign this variable to an object property to work with.
for ($i=0; $i < strlen($string); $i++)
{
    $string_array[$i] = $string[$i];
}

echo implode( $string_array ); //prints 'foobar'

echo '<br>' . PHP_EOL;

echo implode( array_reverse( $string_array ) ); //prints 'raboof'

You could easily simplify your palindrome logic by doing this:

//in your case, this would probably be it's own method,
//using the aforementioned class property made in the constructor

$is_palindrome = implode( $string_array ) === implode( array_reverse( $string_array ) );
mopsyd
  • 1,877
  • 3
  • 20
  • 30
  • What if a var contains an array of sentence.like var = array("hi","Hello","Ssup"); – Ked Nov 26 '17 at 03:24
  • Then you need to run the above once for each string. – mopsyd Nov 26 '17 at 03:24
  • Or if you want to interpret them as one string, use `implode` – mopsyd Nov 26 '17 at 03:25
  • Yes. Code does not ever guess for you. It does what you tell it to do and only that. So if you write a function that handles a string and then you give it anything other than a string, it chokes. – mopsyd Nov 26 '17 at 03:28
  • Just put that code inside a function that accepts an array as a parameter. Then just use a `foreach` loop to run the code for each string in the array. – icecub Nov 26 '17 at 03:31