Skip to content

Commit

Permalink
reverse string using stack in c
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaRithik committed Oct 31, 2018
1 parent 4029e5f commit 1a171f9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions rev_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<stdio.h>
#include<string.h>

//define maximum up to 20
#define MAX 20

int top = -1;
char stack[MAX];

/*Begin of push*/
char push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
stack[++top] =item;
}
/*End of push*/

/*Begin of pop*/
char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return stack[top--];
}
/*End of pop*/

/*Begin of main*/
int main()
{
char str[20];
int i;
printf("Enter the string : " );
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);
return 0;

}

0 comments on commit 1a171f9

Please sign in to comment.