Reversing a string with Iterators - C++ STL

C++ examples for STL:string

Description

Reversing a string with Iterators

Demo Code

#include <iostream>
#include <string>

void printReverse(std::string&);

int main(int argc, const char* argv[]) {
    std::cout << "Enter a string: ";

    std::string base;/*w w  w .  j av  a2 s  .  c om*/
    std::getline(std::cin, base);

    printReverse(base);

    return 0;
}
// prints a given string in reverse using iterators
void printReverse(std::string& base) {
    std::string::reverse_iterator rit = base.rbegin();

    while (rit != base.rend()) {
        std::cout << *(rit++);
    }
    std::cout << std::endl;
}

Result


Related Tutorials