{"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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20reverse%20a%20string%20using%20stack%0A%20%0A%23%20Function%20to%20create%20an%20empty%20stack.%20It%20initializes%20size%20of%20stack%20as%200%0Adef%20createStack()%3A%0A%20%20%20%20stack%3D%5B%5D%0A%20%20%20%20return%20stack%0A%20%0A%23%20Function%20to%20determine%20the%20size%20of%20the%20stack%0Adef%20size(stack)%3A%0A%20%20%20%20return%20len(stack)%0A%20%0A%23%20Stack%20is%20empty%20if%20the%20size%20is%200%0Adef%20isEmpty(stack)%3A%0A%20%20%20%20if%20size(stack)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20true%0A%20%0A%23%20Function%20to%20add%20an%20item%20to%20stack%20.%20It%20increases%20size%20by%201%20%20%20%20%0Adef%20push(stack%2Citem)%3A%0A%20%20%20%20stack.append(item)%0A%20%0A%23Function%20to%20remove%20an%20item%20from%20stack.%20It%20decreases%20size%20by%201%0Adef%20pop(stack)%3A%0A%20%20%20%20if%20isEmpty(stack)%3A%20return%0A%20%20%20%20return%20stack.pop()%0A%20%0A%23%20A%20stack%20based%20function%20to%20reverse%20a%20string%0Adef%20reverse(string)%3A%0A%20%20%20%20n%20%3D%20len(string)%0A%20%20%20%20%20%0A%20%20%20%20%23%20Create%20a%20empty%20stack%0A%20%20%20%20stack%20%3D%20createStack()%0A%20%0A%20%20%20%20%23%20Push%20all%20characters%20of%20string%20to%20stack%0A%20%20%20%20for%20i%20in%20range(0%2Cn%2C1)%3A%0A%20%20%20%20%20%20%20%20push(stack%2Cstring%5Bi%5D)%0A%20%0A%20%20%20%20%23%20Making%20the%20string%20empty%20since%20all%20characters%20are%20saved%20in%20stack%20%20%20%20%0A%20%20%20%20string%3D%22%22%0A%20%0A%20%20%20%20%23%20Pop%20all%20characters%20of%20string%20and%20put%20them%20back%20to%20string%0A%20%20%20%20for%20i%20in%20range(0%2Cn%2C1)%3A%0A%20%20%20%20%20%20%20%20string%2B%3Dpop(stack)%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20return%20string%0A%20%20%20%20%20%0A%23%20Driver%20program%20to%20test%20above%20functions%0Astring%3D%22GeeksQuiz%22%0Astring%20%3D%20reverse(string)%0Aprint(%22Reversed%20string%20is%20%22%20%2B%20string)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Sunny%20Karira%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20reverse%20a%20string%20without%20stack%0A%20%0A%23%20Function%20to%20reverse%20a%20string%0Adef%20reverse(string)%3A%0A%20%20%20%20string%20%3D%20string%5B%3A%3A-1%5D%0A%20%20%20%20return%20string%0A%20%0A%23%20Driver%20program%20to%20test%20above%20functions%0Astring%20%3D%20%22abc%22%0Astring%20%3D%20reverse(string)%0Aprint(%22Reversed%20string%20is%20%22%20%2B%20string)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Sunny%20Karira\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Reversed string is cba<\/pre>\n[ad type=\u201dbanner\u201d]\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}]}}