Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created January 14, 2020 19:44
Show Gist options
  • Save misterpoloy/8aa98de2ad8b4c12f418973e80658410 to your computer and use it in GitHub Desktop.
Save misterpoloy/8aa98de2ad8b4c12f418973e80658410 to your computer and use it in GitHub Desktop.
String Reverse using Stack with c++
#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