C++ examples for Function:Recursive Function
Recursively Print a String Backward
#include <iostream> #include <string> void stringReverse(const std::string&, int start = -1); int main(int argc, const char* argv[]) { std::string string1 = "This is a string1"; std::string string2 = "String 2 looks like this"; std::cout << string1 << std::endl; stringReverse(string1, string1.length()); std::cout << std::endl;//ww w.ja v a 2s.c om std::cout << string2 << std::endl; stringReverse(string2); return 0; } // recursively prints a string in reverse void stringReverse(const std::string& st, int start) { // account for lack of start index if (start == -1) start = st.length(); // base case if (start == 0) { std::cout << st[start] << std::endl; return; } std::cout << st[start]; stringReverse(st, --start); }