Created
January 14, 2020 19:44
-
-
Save misterpoloy/8aa98de2ad8b4c12f418973e80658410 to your computer and use it in GitHub Desktop.
String Reverse using Stack with c++
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 <iostream> | |
#include <stack> | |
char* reverse(char* str, int size) { | |
std::stack<char> myText; | |
// Don't count the null character array | |
for (int i = 0; i < size - 1; i++) { | |
myText.push(str[i]); | |
} | |
// Don't count the null character array | |
for (int i = 0; i < size - 1; i++) { | |
str[i] = myText.top(); | |
myText.pop(); | |
} | |
return str; | |
} | |
void print(char* text) { | |
int i = 0; | |
while (text[i] != '\0') { | |
std::cout << text[i] << std::endl; | |
i++; | |
} | |
} | |
int main() { | |
char str[] = "bacon"; | |
reverse(str, sizeof(str)); | |
print(str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment