#include <iostream>
#include <string>
#include <vector>
void CStringSwitcher( )
{
std:: vector < std:: string > inputs;
size_t totalLength = 0 ;
std:: cout << "Enter a sentence on each line. Input a 0 to stop." << std:: endl ;
inputs.reserve ( 16 ) ;
for ( ; /* until break */ ; ) {
std:: cout << ">" ;
std:: string input;
getline( std:: cin , input) ;
if ( input == "0" )
break ;
inputs.push_back ( input) ;
totalLength + = input.length ( ) + 1 ; // for the '\n'
}
std:: string reversed = "" ;
reversed.reserve ( totalLength) ; // eliminate allocations
for ( auto inputsIt = inputs.rbegin ( ) ; inputsIt ! = inputs.rend ( ) ; ++ inputsIt) {
const std:: string & input = * ( inputsIt) ;
#ifndef REAL_CODE
// educational, Do-It-Yourself way
const size_t length = input.length ( ) ;
for ( size_t i = 0 ; i < length; ++ i) {
reversed + = input[ length - 1 - i] ;
}
#else
reversed.append ( input.rbegin ( ) , input.rend ( ) ) ;
#endif
reversed + = '\n ' ;
}
std:: cout << std:: endl << reversed << std:: endl ;
// don't pause, set a break point at the end of the function
// or run without debugging.
return ;
}
int main( int argc, const char * argv[ ] )
{
CStringSwitcher( ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKdm9pZCBDU3RyaW5nU3dpdGNoZXIoKQp7CiAgICBzdGQ6OnZlY3RvcjxzdGQ6OnN0cmluZz4gaW5wdXRzOwoJc2l6ZV90IHRvdGFsTGVuZ3RoID0gMDsKCQogICAgc3RkOjpjb3V0IDw8ICJFbnRlciBhIHNlbnRlbmNlIG9uIGVhY2ggbGluZS4gSW5wdXQgYSAwIHRvIHN0b3AuIiA8PCBzdGQ6OmVuZGw7CglpbnB1dHMucmVzZXJ2ZSgxNik7CgoJZm9yICggOyAvKiB1bnRpbCBicmVhayAqLyA7ICkgewoJCXN0ZDo6Y291dCA8PCAiPiI7CgkJc3RkOjpzdHJpbmcgaW5wdXQ7CgkJZ2V0bGluZShzdGQ6O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XSkKewoJQ1N0cmluZ1N3aXRjaGVyKCk7CgoJcmV0dXJuIDA7Cn0K
stdin
SGVsbG8gZGFya25lc3MsIG15IG9sZCBmcmllbmQsCkkndmUgY29tZSB0byB0YWxrIHdpdGggeW91IGFnYWluLApCZWNhdXNlIGEgdmlzaW9uIHNvZnRseSBjcmVlcGluZywKQW5kIHRoZSB2aXNpb24gdGhhdCB3YXMgcGxhbnRlZCBpbiBteSBicmFpbgpTdGlsbCByZW1haW5zClNvbWV0aGluZyBzb21ldGhpbmcgZGFyayBzaWRlCjAKCg==
Hello darkness, my old friend,
I've come to talk with you again,
Because a vision softly creeping,
And the vision that was planted in my brain
Still remains
Something something dark side
0