#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
#include <cstring> // for str functions

char *reverse_string(char *str)
{
    static char buffer[300];
    int len, i, j;
    if (str == NULL)
            return (NULL);
    len = strlen(str);  //should check that len < 300

    for (i=(len-1), j=0; i>=0; i--, j++)
    {
            buffer[j] = str[i];
    }
    buffer[j] = '\0';   // null terminator
    return buffer;    // returned pointer points to static memory
}

void reverse_string_example()
{
    char full_name[100];
    
    cout << "Enter your full name" << endl;
    cin.getline(full_name, 100);
    cout << "Full name: " << full_name << endl;
    
    char *reversed = reverse_string(full_name);
    //char *ptr = reverse_string("Fred"); // beware of the static buffer!
    cout << "Reversed: " << reversed << endl;
    //cout << "Ptr: " << ptr << endl;
    
    // Will the above work with an empty string?
    char *emptyStr = ""; // strlen is 0, so i in above for loop starts at -1
    char *revEmpty = reverse_string(emptyStr);
    cout << "RevEmpty: " << revEmpty << endl;
}

int main (int argc, const char * argv[])
{
    reverse_string_example();
    return 0;
}

