{"id":26863,"date":"2017-12-26T20:29:13","date_gmt":"2017-12-26T14:59:13","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26863"},"modified":"2017-12-26T20:29:13","modified_gmt":"2017-12-26T14:59:13","slug":"java-algorithm-reverse-string-using-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-reverse-string-using-stack\/","title":{"rendered":"Java 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>Java Programming:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F*%20Java%20program%20to%20reverse%20%0A%20%20%20String%20using%20Stack%20*%2F%0A%20%20%20%20%20%0Aimport%20java.util.*%3B%0A%20%20%0A%2F%2Fstack%0Aclass%20Stack%0A%7B%0A%20%20%20%20int%20size%3B%0A%20%20%20%20int%20top%3B%0A%20%20%20%20char%5B%5D%20a%3B%20%0A%20%20%20%0A%20%20%20%20%2F%2Ffunction%20to%20check%20if%20stack%20is%20empty%0A%20%20%20%20boolean%20isEmpty()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20(top%20%3C%200)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%20%0A%20%20%20%20Stack(int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20top%20%3D%20-1%3B%0A%20%20%20%20%20%20%20%20size%20%3D%20n%3B%0A%20%20%20%20%20%20%20%20a%20%3D%20new%20char%5Bsize%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%0A%20%20%20%20%2F%2Ffunction%20to%20push%20element%20in%20Stack%0A%20%20%20%20boolean%20push(char%20x)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(top%20%3E%3D%20size)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Stack%20Overflow%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20a%5B%2B%2Btop%5D%20%3D%20x%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%0A%20%20%20%20%2F%2Ffunction%20to%20pop%20element%20from%20stack%0A%20%20%20%20char%20pop()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(top%20%3C%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Stack%20Underflow%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20char%20x%20%3D%20a%5Btop\u2013%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20x%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%20%20%0A%20%20%0A%2F%2F%20Driver%20code%0Aclass%20Main%0A%7B%0A%20%20%20%20%2F%2Ffunction%20to%20reverse%20the%20string%0A%20%20%20%20public%20static%20void%20reverse(StringBuffer%20str)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20stack%20of%20capacity%20%0A%20%20%20%20%20%20%20%20%2F%2F%20equal%20to%20length%20of%20string%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20str.length()%3B%0A%20%20%20%20%20%20%20%20Stack%20obj%20%3D%20new%20Stack(n)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Push%20all%20characters%20of%20string%20%0A%20%20%20%20%20%20%20%20%2F%2F%20to%20stack%0A%20%20%20%20%20%20%20%20int%20i%3B%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20obj.push(str.charAt(i))%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Pop%20all%20characters%20of%20string%20%0A%20%20%20%20%20%20%20%20%2F%2F%20and%20put%20them%20back%20to%20str%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20char%20ch%20%3D%20obj.pop()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20str.setCharAt(i%2Cch)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20%0A%20%20%20%20%20%20%0A%20%20%20%20%2F%2Fdriver%20function%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2Fcreate%20a%20new%20string%0A%20%20%20%20%20%20%20%20StringBuffer%20%20s%3D%20new%20StringBuffer(%22GeeksforGeeks%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fcall%20reverse%20method%0A%20%20%20%20%20%20%20%20reverse(s)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fprint%20the%20reversed%20string%0A%20%20%20%20%20%20%20%20System.out.println(%22Reversed%20string%20is%20%3A%20%22%20%2B%20s)%3B%0A%20%20%20%20%7D%0A%7D\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<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[ad type=\u201dbanner\u201d]\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Evaluation of Postfix Expression &#8211; Data Structure -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,2139,79607],"tags":[80138,80129,80134,80135,80137,80133,80136,80130,80131,80132],"class_list":["post-26863","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-java","category-stack","tag-how-to-reverse-a-stack-in-java","tag-program-to-reverse-a-string-using-stack-in-java","tag-reverse-a-number-using-stack-in-c","tag-reverse-a-stack-using-recursion-in-c","tag-reverse-a-string-using-stack-c","tag-reverse-a-string-using-stack-in-python","tag-reverse-word-using-stack-java","tag-stack-java","tag-stack-string-c","tag-use-a-stack-to-reverse-the-words-of-a-sentence"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26863","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=26863"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26863\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}