/*
 * @topic T0M292 Reverse string demo
 * @brief Reverse characters using array [subscript] syntax
*/

#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

// Get characters and store them in memory buffer
// pstr is the pointer to the memory buffer.
// This function is using array [subscript] syntax
// with pointer to access memory buffer.
void get_input( char* pstr )
{
    char ch;
    int idx = 0;
    for ( ; cin.get( ch ); ++idx ) {
        if ( ch == '\n' ) {
            // user pressed the enter key
            // terminate user input by null character:
            pstr[ idx ] = '\x0';
            return;
        }
        pstr[ idx ] = ch;
    }
}//get_input

int main()
{
    for (;;) {
        cout << "Enter text to reverse: ";
        //string str;
        const int ARRAY_SIZE = 1024;
        char str[ ARRAY_SIZE ];
        get_input( str );
        if ( !strncmp( str, "quit", ARRAY_SIZE ) ) {
            break;
        }
        reverse( str, str + strnlen( str, ARRAY_SIZE ) );
        cout << "[" << str << "]\n";
    }//forever

    system( "pause" );
    return 0;
}

