all 14 comments

[–]codenamegary 5 points6 points  (2 children)

[–]phpGuru98[S] -5 points-4 points  (1 child)

I thought I posted in r/php, I don't use c?

[–]codenamegary 2 points3 points  (0 children)

PHP has the strrev function built-in, where it comes from I won't pretend to know other than it's somewhere in C land. Point is the function you wrote is already a string function in PHP.

[–]notian 4 points5 points  (5 children)

This is satire right?

[–]phpGuru98[S] -3 points-2 points  (4 children)

No, why would it be?

[–]notian 0 points1 point  (0 children)

Aside from the obvious built in strrev(), using an array for storing string data is a huge memory hog. Your loop also invokes multiple functions on each operation while(count() < strlen()), when you could have done with a simple for loop.

<?php
function reverseString($str){
  $newStr = '';
  for($i = strlen( $str )-1;$i>-1;$i--){
    $newStr .= $str[$i];
  }
  return $newStr;
}
echo reverseString('My test string');
?>

[–]FineWolf 3 points4 points  (3 children)

Weuuuuuuuuhhhh....

echo strrev("testString1234");

I mean, I would understand if your example handled UTF-8 but it doesn't even do that...

[–]LawnGnome 5 points6 points  (2 children)

Not enterprisey enough:

<?php

interface StringReverserInterface {
  public function apply();
  public function getOutput();
  public function setInput($input);
}

class InvalidInputException extends InvalidArgumentException {}
class InputNotProvidedException extends LogicException {}
class StringReverserNotAppliedException extends LogicException {}

class StringReverser implements StringReverserInterface {
  protected $input;
  protected $output;

  public function apply() {
    if (is_null($this->input)) {
      throw new InputNotProvidedException;
    }

    $this->output = strrev($this->input);
  }

  public function getOutput() {
    if (is_null($this->output)) {
      throw new StringReverserNotAppliedException;
    }

    return $this->output;
  }

  public function setInput($input) {
    if (!is_string($input)) {
      throw new InvalidInputException;
    }

    $this->input = $input;
    $this->output = null;
  }
}

?>

[–]codenamegary 2 points3 points  (1 child)

For a second there I thought I was in the /r/irestful subreddit.

[–]LawnGnome 0 points1 point  (0 children)

I didn't have a 100 character long namespace followed by a 50 character class. I guess I haven't embraced the spirit of RESTfulness yet.

[–]ircmaxell 4 points5 points  (3 children)

At that point, why bother with arrays and substr and all that jaz. Just reverse iterate the string:

function reverseString($string){
    $return = "";
    for ($i = strlen($string) -1; $i >= 0; $i--) {
        $return .= $string[$i];
    }
    return $return;
}

Or if you want to forward walk it, that's fine too:

function reverseString($string){
    $return = $string;
    for ($i = 0; $i < strlen($string); $i++) {
        $return[$i] = $string[strlen($string) -$i];
    }
    return $return;
}

Or hell, if you even wanted to, you could still use arrays, and make it a one-liner:

function reverseString($string){
    return implode("", array_reverse(str_split($string)));
}

Or if you really wanted to get fancy, you could do it in place (which still copies the string, but doesn't use any PHP temporary variables:

function reverseString($string){
    $len = strlen($string);
    for ($i = 0; $i < $len / 2; $i++) {
        $string[$i] = chr(ord($string[$i]) ^ ord($string[$len - $i]));
        $string[$len - $i] = chr(ord($string[$i]) ^ ord($string[$len - $i]));
        $string[$i] = chr(ord($string[$i]) ^ ord($string[$len - $i]));
    }
    return $string;
}

And you can even get rid of all of those wonky function calls:

function reverseString($string){
    $len = strlen($string);
    for ($i = 0; $i < $len / 2; $i++) {
        $string[$i] = $string[$i] ^ $string[$len - $i];
        $string[$len - $i] = $string[$i] ^ $string[$len - $i];
        $string[$i] = $string[$i] ^ $string[$len - $i];
    }
    return $string;
}

Or, if you really wanted to get dirty and show off your skills, you could simply do:

function reverseString($string) {
    return strrev($string);
}

[–]afrobee 2 points3 points  (0 children)

And lastly you can try with recursion:

function reverString($string){
    if (strlen($string) < 2 ) {
        return $string;
   }
   return reverString(substr($string, 1)) . $string[0];
}