{"id":26861,"date":"2017-12-26T20:33:45","date_gmt":"2017-12-26T15:03:45","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26861"},"modified":"2017-12-26T20:33:45","modified_gmt":"2017-12-26T15:03:45","slug":"c-algorithm-reverse-string-using-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-reverse-string-using-stack\/","title":{"rendered":"C 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>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program to reverse a string using stack<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>#include &lt;limits.h&gt;<br\/> <br\/>\/\/ A structure to represent a stack<br\/>struct Stack<br\/>{<br\/>    int top;<br\/>    unsigned capacity;<br\/>    char* array;<br\/>};<br\/> <br\/>\/\/ function to create a stack of given capacity. It initializes size of<br\/>\/\/ stack as 0<br\/>struct Stack* createStack(unsigned capacity)<br\/>{<br\/>    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));<br\/>    stack-&gt;capacity = capacity;<br\/>    stack-&gt;top = -1;<br\/>    stack-&gt;array = (char*) malloc(stack-&gt;capacity * sizeof(char));<br\/>    return stack;<br\/>}<br\/> <br\/>\/\/ Stack is full when top is equal to the last index<br\/>int isFull(struct Stack* stack)<br\/>{   return stack-&gt;top == stack-&gt;capacity - 1; }<br\/> <br\/>\/\/ Stack is empty when top is equal to -1<br\/>int isEmpty(struct Stack* stack)<br\/>{   return stack-&gt;top == -1;  }<br\/> <br\/>\/\/ Function to add an item to stack.  It increases top by 1<br\/>void push(struct Stack* stack, char item)<br\/>{<br\/>    if (isFull(stack))<br\/>        return;<br\/>    stack-&gt;array[++stack-&gt;top] = item;<br\/>}<br\/> <br\/>\/\/ Function to remove an item from stack.  It decreases top by 1<br\/>char pop(struct Stack* stack)<br\/>{<br\/>    if (isEmpty(stack))<br\/>        return INT_MIN;<br\/>    return stack-&gt;array[stack-&gt;top--];<br\/>}<br\/> <br\/>\/\/ A stack based function to reverese a string<br\/>void reverse(char str[])<br\/>{<br\/>    \/\/ Create a stack of capacity equal to length of string<br\/>    int n = strlen(str);<br\/>    struct Stack* stack = createStack(n);<br\/> <br\/>    \/\/ Push all characters of string to stack<br\/>    int i;<br\/>    for (i = 0; i &lt; n; i++)<br\/>        push(stack, str[i]);<br\/> <br\/>    \/\/ Pop all characters of string and put them back to str<br\/>    for (i = 0; i &lt; n; i++)<br\/>        str[i] = pop(stack);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    char str[] = &quot;GeeksQuiz&quot;;<br\/> <br\/>    reverse(str);<br\/>    printf(&quot;Reversed string is %s&quot;, str);<br\/> <br\/>    return 0;<br\/>}<\/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>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ C program to reverse a string without using stack<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>\/\/ A utility function to swap two characters<br\/>void swap(char *a, char *b)<br\/>{<br\/>    char temp = *a;<br\/>    *a = *b;<br\/>    *b = temp;<br\/>}<br\/> <br\/>\/\/ A stack based function to reverese a string<br\/>void reverse(char str[])<br\/>{<br\/>    \/\/ get size of string<br\/>    int n = strlen(str), i;<br\/> <br\/>    for (i = 0; i &lt; n\/2; i++)<br\/>        swap(&amp;str[i], &amp;str[n-i-1]);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    char str[] = &quot;abc&quot;;<br\/> <br\/>    reverse(str);<br\/>    printf(&quot;Reversed string is %s&quot;, str);<br\/> <br\/>    return 0;<br\/>}<\/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> C 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,82927,1],"tags":[80224,80219,80226,80225,80134,80135,80216,80133,80228,80223,80222,80220,80218,80221,80227,80217],"class_list":["post-26861","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-2","category-coding","tag-c-program-to-reverse-a-stack-without-using-recursion","tag-c-program-using-stack-to-display-reverse-of-the-string","tag-java-program-to-reverse-a-stack-using-recursion","tag-recursion-using-stack-in-c","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-reverse-an-array-using-stack-in-c","tag-reverse-stack-c","tag-reverse-stack-using-two-stacks","tag-reverse-words-in-a-string-using-stack","tag-reversing-a-string-using-stack-in-cpp","tag-sort-a-stack-using-recursion","tag-sort-a-stack-using-recursion-in-c","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\/26861","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=26861"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26861\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}