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.


Share this

0 Comment to "C Program To Reverse a String using Stack"

Post a Comment