{"id":26906,"date":"2017-12-26T21:14:28","date_gmt":"2017-12-26T15:44:28","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26906"},"modified":"2017-12-26T21:14:28","modified_gmt":"2017-12-26T15:44:28","slug":"python-algorithm-reverse-string-using-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-reverse-string-using-stack\/","title":{"rendered":"Python Algorithm &#8211; Reverse a String using Stack"},"content":{"rendered":"<p>Given a string, reverse it using stack. For example \u201cGeeksQuiz\u201d should be converted to \u201cziuQskeeG\u201d.<\/p>\n<p>Following is simple algorithm to reverse a string using stack.<\/p>\n<pre>1) Create an empty stack.\r\n2) One by one push all characters of string to stack.\r\n3) One by one pop all characters from stack and put \r\n   them back to string.<\/pre>\n<p>Following are C and Python programs that implements above algorithm.<\/p>\n<p><strong>Python \u00a0Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to reverse a string using stack<br\/> <br\/># Function to create an empty stack. It initializes size of stack as 0<br\/>def createStack():<br\/>    stack=[]<br\/>    return stack<br\/> <br\/># Function to determine the size of the stack<br\/>def size(stack):<br\/>    return len(stack)<br\/> <br\/># Stack is empty if the size is 0<br\/>def isEmpty(stack):<br\/>    if size(stack) == 0:<br\/>        return true<br\/> <br\/># Function to add an item to stack . It increases size by 1    <br\/>def push(stack,item):<br\/>    stack.append(item)<br\/> <br\/>#Function to remove an item from stack. It decreases size by 1<br\/>def pop(stack):<br\/>    if isEmpty(stack): return<br\/>    return stack.pop()<br\/> <br\/># A stack based function to reverse a string<br\/>def reverse(string):<br\/>    n = len(string)<br\/>     <br\/>    # Create a empty stack<br\/>    stack = createStack()<br\/> <br\/>    # Push all characters of string to stack<br\/>    for i in range(0,n,1):<br\/>        push(stack,string[i])<br\/> <br\/>    # Making the string empty since all characters are saved in stack    <br\/>    string=&quot;&quot;<br\/> <br\/>    # Pop all characters of string and put them back to string<br\/>    for i in range(0,n,1):<br\/>        string+=pop(stack)<br\/>         <br\/>    return string<br\/>     <br\/># Driver program to test above functions<br\/>string=&quot;GeeksQuiz&quot;<br\/>string = reverse(string)<br\/>print(&quot;Reversed string is &quot; + string)<br\/> <br\/># This code is contributed by Sunny Karira<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Reversed string is ziuQskeeG<\/pre>\n<p><strong><br \/>\nTime Complexity: <\/strong>O(n) where n is number of characters in stack.<br \/>\n<strong>Auxiliary Space: <\/strong>O(n) for stack.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>A string can also be reversed without using any auxiliary space. Following C and Python programs to implement reverse without using stack.<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program to reverse a string without stack<br\/> <br\/># Function to reverse a string<br\/>def reverse(string):<br\/>    string = string[::-1]<br\/>    return string<br\/> <br\/># Driver program to test above functions<br\/>string = &quot;abc&quot;<br\/>string = reverse(string)<br\/>print(&quot;Reversed string is &quot; + string)<br\/> <br\/># This code is contributed by Sunny Karira<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Reversed string is cba<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm &#8211; Evaluation of Postfix Expression &#8211; Data Structure &#8211; Given a string, reverse it using stack. For example \u201cGeeksQuiz\u201d should be converted <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,79607],"tags":[80281,80219,80283,80284,80134,80135,80216,80133,80282,80217],"class_list":["post-26906","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-stack","tag-algorithm-to-reverse-a-string-using-stack-in-data-structure","tag-c-program-using-stack-to-display-reverse-of-the-string","tag-data-structure-to-reverse-string","tag-data-structure-to-reverse-string-without-stack","tag-reverse-a-number-using-stack-in-c","tag-reverse-a-stack-using-recursion-in-c","tag-reverse-a-string-using-stack-in-java","tag-reverse-a-string-using-stack-in-python","tag-write-a-simple-program-to-reverse-a-string-using-a-stack","tag-write-an-algorithm-to-reverse-an-input-string-of-characters-using-a-stack"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26906"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26906\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}