C Program to Reverse a String using Stack:
#include<stdio.h>
#include<string.h>
void push(char);
char pop();
char stk[20];
int top=-1;
void main()
{
char input[20],output[20];
int i;
printf("\n Enter the word you want to reverse:");
gets(input);
for(i=0;i<strlen(input);i++)
push(input[i]);
for(i=0;i<strlen(input);i++)
output[i]=pop();
puts(output);
}
void push(char x)
{
top++;
stk[top]=x;
}
char pop()
{
return(stk[top--]);
}
#include<stdio.h>
#include<string.h>
void push(char);
char pop();
char stk[20];
int top=-1;
void main()
{
char input[20],output[20];
int i;
printf("\n Enter the word you want to reverse:");
gets(input);
for(i=0;i<strlen(input);i++)
push(input[i]);
for(i=0;i<strlen(input);i++)
output[i]=pop();
puts(output);
}
void push(char x)
{
top++;
stk[top]=x;
}
char pop()
{
return(stk[top--]);
}
OUTPUT |
No comments:
Post a Comment