-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoding_string.c
More file actions
82 lines (80 loc) · 1.19 KB
/
encoding_string.c
File metadata and controls
82 lines (80 loc) · 1.19 KB
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
#include<stdio.h>
#include<string.h>
#define size 100
int is[size];
char cs[size];
int itop=-1,ctop=-1;
void ipush(int n)
{
is[++itop]=n;
}
void cpush(char ch)
{
cs[++ctop]=ch;
}
int ipop()
{
return is[itop--];
}
char cpop()
{
return cs[ctop--];
}
void decode(char str[])
{
char temp[size],result[size],ch;
int i,j,count;
for (i=0;str[i]!='\0';i++)
{
count = 0;
if (isdigit(str[i]))
{
count=str[i]-48;
ipush(count);
}
else if (str[i] == ']')
{
count = ipop();
strcpy(temp,"");
j=0;
while ((ch=cpop())!='[' )
{
temp[j++] = ch;
}
temp[j]='\0';
strrev(temp);
for (j = 0; j < count; j++)
strcat(result,temp);
for (j= 0; result[j]!='\0'; j++)
cpush(result[j]);
strcpy(result,"");
}
else if (str[i] == '[')
{
if (isdigit(str[i-1]))
cpush(str[i]);
else
{
cpush(str[i]);
ipush(1);
}
}
else
cpush(str[i]);
}
j=0;
while (ctop!=-1)
{
result[j++] =cpop();
}
result[j]='\0';
printf("\n%s",strrev(result));
}
int main()
{
char str[100];
printf("Enter a String "); //"2[a2[b3[c]]]";
gets(str);
decode(str);
return 0;
}