Last active
November 26, 2015 00:01
-
-
Save georgymh/c7c329d894f721b2e82e to your computer and use it in GitHub Desktop.
Reverses a string recursively and non-recursively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
using namespace std; | |
void reverseStr(string& str); | |
string recursiveReverseStr(string& str); | |
int main() | |
{ | |
string str = "Georgy"; | |
cout << "Word to reverse: " << str << endl; | |
cout << "Non-recursive: "; | |
reverseStr(str); | |
cout << str; | |
cout << endl << "Recursive: "; | |
str = recursiveReverseStr(str); | |
cout << str; | |
cout << endl; | |
system("Pause"); | |
return 0; | |
} | |
void reverseStr(string& str) | |
{ | |
int size = static_cast<int>(str.size()); | |
for (int i = 0; i < size / 2; i++) | |
{ | |
char temp = str[i]; | |
str[i] = str[size - i - 1]; | |
str[size - i - 1] = temp; | |
} | |
} | |
string recursiveReverseStr(string& str) | |
{ | |
int size = static_cast<int>(str.size()); | |
if (size == 0) | |
return ""; | |
else | |
return str[size - 1] + recursiveReverseStr(str.substr(0, size-1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment