/*
* @topic T0M294 Reverse string demo
* @brief Reverse characters using pointer arithmetic
*/
#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 pointer arithmetic
// to access memory buffer.
void get_input( char* pstr )
{
char ch;
for ( ; cin.get( ch ); ++pstr ) {
if ( ch == '\n' ) {
// user pressed the enter key
// terminate user input by null character:
*pstr = '\x0';
return;
}
*pstr = 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;
}//main