diff --git a/Beginner/C++/Reverse sentence b/Beginner/C++/Reverse sentence new file mode 100644 index 0000000..599c4b2 --- /dev/null +++ b/Beginner/C++/Reverse sentence @@ -0,0 +1,37 @@ +//Reverse a sentence using recursion. + + +#include +using namespace std; + +// function prototype +void reverse(const string& a); + +int main() { + string str; + + cout << " Please enter a string " << endl; + getline(cin, str); + + // function call + reverse(str); + + return 0; +} + +// function definition +void reverse(const string& str) { + + // store the size of the string + size_t numOfChars = str.size(); + + if(numOfChars == 1) { + cout << str << endl; + } + else { + cout << str[numOfChars - 1]; + + // function recursion + reverse(str.substr(0, numOfChars - 1)); + } +}