Implement the reverseString method, which accepts a string and returns its reverse.

Beginner Level
String Manipulation Loop
Write a method named reverseString that accepts a string as input and returns its reverse. You must not use built-in reverse functions; instead, implement the logic manually using loops or string manipulation techniques.
Example 1
Input:
s (string) = hello
Return:
(string) olleh
Example 2
Input:
s (string) = world
Return:
(string) dlrow

To reverse a string without using built-in reverse functions, follow these steps:

  1. Check for Empty or Null Input
    If the input string is empty or null, return it directly or handle it as an edge case.

  2. Determine String Length
    Calculate the number of characters in the string.

  3. Create a New String/Array for the Result
    Allocate a new variable (or array) to hold the reversed characters.

  4. Reverse the Characters Using a Loop
    Use a loop to copy characters from the end of the original string to the beginning of the new one.

  5. Terminate the New String if Required
    Some languages require you to manually add a string terminator (like \0 in C), others don’t.

  6. Return the Reversed String

Pseudocode:

function reverseString(input):
    if input is null or empty:
        return input

    length = get length of input
    result = create empty string of size length

    for i from 0 to length - 1:
        result[i] = input[length - 1 - i]

    return result
Run your code to see the result.