Reversing a string with Iterators using Recursion - C++ STL

C++ examples for STL:iterator

Description

Reversing a string with Iterators using Recursion

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;//from ww w .j a v a2  s  .c  o  m
    std::getline(std::cin, base);

    printReverse(base);
    std::cout << std::endl;

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

    if (rit == base.rend()) return;

    std::cout << *(rit++);

    printReverse(base);
}

Result


Related Tutorials