Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Beginner/C++/Reverse sentence
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//Reverse a sentence using recursion.


#include <iostream>
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));
}
}