Skip to content

Instantly share code, notes, and snippets.

@aykutyaman
Created August 28, 2018 04:46
Show Gist options
  • Save aykutyaman/9b31bb59980b4c37165a07c25995a999 to your computer and use it in GitHub Desktop.
Save aykutyaman/9b31bb59980b4c37165a07c25995a999 to your computer and use it in GitHub Desktop.
// reverse a string using stack data structure
const reverse = s => {
let stack = [];
let reversed = '';
// push all characters of string to stack
for (var i = 0; i < s.length; i++) {
stack.push(s[i]);
}
// pop all characters of string and put them into reversed string
while (stack.length > 0) {
reversed += stack.pop();
}
return reversed;
}
console.log(reverse('aykut') === 'tukya');
console.log(reverse('a b c') === 'c b a');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment