-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
88 lines (69 loc) · 1.47 KB
/
main.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define String_Max 82
typedef struct {
int max;
int ptr;
char **stk;
} StringsStack;
int Initialize(StringsStack *s, int max) {
s->ptr = 0;
if ((s->stk = calloc(max, sizeof(char *))) == NULL) {
s->max = 0;
return -1;
}
s->max = max;
return 0;
}
void Terminate(StringsStack *s) {
free(s->stk);
}
int Push(StringsStack *s, char *x) {
if (s->ptr >= s->max) return -1;
s->stk[s->ptr++] = x;
return 0;
}
int Pop(StringsStack *s, char **x) {
if (s->ptr <= 0) return -1;
*x = s->stk[--s->ptr];
return 0;
}
int IsEmpty(const StringsStack *s) {
return s->ptr <= 0;
}
void recur3(char *st) {
StringsStack stk;
char *temp = st;
if (Initialize(&stk, 100) == -1) {
puts("Error: Failed Initialize.");
return;
}
while (1) {
while (strlen(temp) > 0) {
if (Push(&stk, temp) == -1) {
puts("Error: Failed Push.");
return;
}
temp += (strlen(temp) > 1) ? 2 : 1;
}
if (IsEmpty(&stk)) {
break;
}
if (Pop(&stk, &temp) == -1) {
puts("Error: Failed Pop.");
return;
}
printf("%c", *temp);
temp += 1;
}
Terminate(&stk);
}
int main(void) {
char x[String_Max];
printf("Input String: ");
scanf("%s", x);
recur3(x);
printf("\n");
return 0;
}