• Discussion
  • PHP Short Code Challenge: String Manipulation

Write a PHP function named reverseAndCapitalize that takes a string as input, reverses the characters, and returns the result in uppercase.

Example:

`function reverseAndCapitalize($input) {
// Your code goes here
}

// Test the function
$result = reverseAndCapitalize("Hello, PHP!");
echo $result; // Should print "!P ,OLLEH"
`
Provide a concise and effective PHP code solution. Thank you!

    Exactly what it says on the tin.

    // Your code goes here
    return strtoupper(strrev($input));

    Because I'm weird (and needed a break from what I was doing at work), I came up with this, just wondering how much I could over-complicate it without getting really, really stupid (like putting the results of the for() loop into another array, and then implode()-ing it. 😉 )

    function reverseAndCapitalize($input) {
      $result = '';
      $characters = str_split($input, 1);
      for($i = count($characters) -1; $i >= 0; $i--) {
        $char = $characters[$i];
        $result .= strtoupper($char);
      }
      return $result;
    }
    

    NogDog really, really stupid

    So using count and str_split instead of strlen and accessing the characters directly is just "really stupid" 😉

    Weedpacket Right...I needed to put it into an array so that I use more of all that memory on my mac mini that was going to waste otherwise. 😆

    a month later

    I know I'm a lightweight here in the presence of giants, but I gotta wonder if someone just got homework help by phrasing the problem as a Code Contest? 😮

    Write a Reply...