/*
* @topic T0M310 Reverse string demo Spring 2017
* @brief C++ reverse characters using array [subscript] syntax
*/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std; // allows cout instead of std::cout
int main()
{
// reverse characters in the string:
// 0123456789
// abcdef
// | |
// | idx_tail = input.length() - 1
// idx_front = 0
string input;
while (
cout << "enter a string: ",
getline( cin, input ) // get entire line of input with spaces
)
{
if ( input.length() == 0 ) {
cout << "nothing to do\n";
continue;
}
size_t idx_tail = input.length() - 1;
size_t idx_front = 0;
for ( ; idx_front < idx_tail; ++idx_front, --idx_tail ) {
char front = input[ idx_front ];
input[ idx_front ] = input[ idx_tail ];
input[ idx_tail ] = front;
}
cout << "reversed string is\n" << input << "\n";
}//while
system("pause");
return 0;
}