C Program To Reverse a String using Stack

C Program To Reverse a String using Stack

Below I share C program to reverse string using stack and the source code is written in c programming language. I also share another string reverse program using inbuilt function 'strrev()' in c language. In this below c program, I can do a reserve of the string with the help of taking character stack.

#include<stdio.h>
#include<string.h>
#define size 20
int top = -1;
char stack[size];

char push(char ch)
{
if(top==(size-1))
printf("Stack is Overflow\n");
else
stack[++top]=ch;
}
char pop()
{
if(top==-1)
printf("Stack is Underflow\n");
else
return stack[top--];
}
int main()
{
char str[20];
int i;
printf("Enter the string : \n" );
gets(str);
for(i=0;i<strlen(str);i++)
{
push(str[i]);
}
for(i=0;i<strlen(str);i++)
{
    str[i]=pop();
}
printf("Reversed string is : ");
puts(str);

}



OUTPUT:

Enter the string:    Reverse of String
Reversed string is: gnirtS fo esreveR




NOTE: You have any problem in the above c program for reverse string using stack do not hesitate and write your problem in the comment box. I will support your problem.


5 comments:

  1. Please explain that program how it is works....

    ReplyDelete
    Replies
    1. In this tutorial, String reverse using stack in this program. I implemented the stack push, pop function. Then, Put each character from string to stack, and as we known stack is working on LIFO. So, it removes the last element first using pop() and prints each character.

      If you don't know about stack please refer: https://www.codeamy.in/2019/07/stack-in-data-structure.html

      Delete
  2. Function shout have returned values (warning) find it how

    ReplyDelete
    Replies
    1. In this tutorial, String reverse using stack in this program. I defined a character array for 20 elements. Your input string is more than 20 characters. So, you can do modifications and take size from the user for your string size.

      Delete
  3. Select the write code which reverses a string.
    1.

    hackreverse(String str)

    Stack stk

    n=size of str

    for i=0 to n

    push(ith character of string)into stk

    String rev = "";

    while (stk is not empty)

    rev = rev + stk.peek()

    return rev

    2.

    hackreverse(String str)

    Stack stk

    n=size of str

    for i=0 to n

    push(ith character of string)into stk

    String rev = "";

    while (stk is not empty)

    rev = rev + stk.pop()

    return rev

    3.

    hackreverse(String str)

    Stack stk

    n=size of str

    for i=0 to n

    push(ith character of string)into stk

    String rev = "";

    while (stk is not empty)

    rev = rev + stk.peek()

    4.

    hackreverse(String str)

    Stack stk

    n=size of str

    for i=0 to n

    push(ith character of string)into stk

    String rev = "";

    while (stk is not empty)

    rev = rev + stk.pop()

    pop element from stk

    return rev

    ReplyDelete

If you have any doubts, Please let me know