-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverse.cpp
51 lines (43 loc) · 1020 Bytes
/
Reverse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* C Program To Reverse String using Stack */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
int top = -1;
char stack[MAX];
char pop();
void push(char);
int main()
{
char str[20];
unsigned int i;
printf("Enter the string : " );
gets(str);
/*Push characters of the string str on the stack */
for(i=0;i<strlen(str);i++)
push(str[i]);
/*Pop characters from the stack and store in string str */
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("\nReversed string is : ");
puts(str);
return 0;
}/*End of main()*/
void push(char item)
{
if(top == (MAX-1))
{
printf("\nStack Overflow\n");
return;
}
stack[++top] =item;
}/*End of push()*/
char pop()
{
if(top == -1)
{
printf("\nStack Underflow\n");
exit(1);
}
return stack[top--];
}/*End of pop()*/