This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
H_Ruban.c
66 lines (66 loc) · 1.56 KB
/
H_Ruban.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
#include <stdio.h>
//#include <stdbool.h>
int main()
{
char str[10000];
char temp ;
int i = 0, j, inc = 0, s, e, c, flag ;
scanf("%s", str);
while(str[i] != '\0')
{
if(str[i] == '[')
inc++ ;
if(str[i] == ']')
inc-- ;
if((str[i] >= 'a') && (str[i] <= 'z'))
str[i] = (char)(((int)(str[i])-97+inc)%26+97); // increment the character (rotation rather) based on count of square brackets
i++ ;
}
do
{
flag = 0 ;
i = 0 ;
c = 0 ; // keeps count of number of nested parentheses
while(str[i] != '\0')
{
if(str[i] == '(')
{
flag = 1 ; // there is a string in parentheses
if(c == 0)
s = i ; // starting index of outermost opening parentheses
c++ ;
}
if(str[i] == ')')
{
c-- ;
if(c == 0) // encountered the outermost closing parentheses
{
e = i ; // ending index of outermost parentheses
str[s++] = '0' ;
str[e--] = '0' ; // remove parentheses after detecting a bracketed string
for(j = s; j <= (s+e)/2; j++) // reverse the outermost string in parentheses
{
temp = str[j];
str[j] = str[s+e-j];
str[s+e-j] = temp ;
}
for(j = s; j <= e; j++) // swap the parentheses
{
if(str[j] == '(')
str[j] = ')' ;
else if(str[j] == ')')
str[j] = '(' ;
}
}
}
i++ ;
}
} while(flag == 1); // repeat the process as long as paratheses exist
i = 0 ;
while(str[i] != '\0')
{
if((str[i] >= 'a') && (str[i] <= 'z')) // print only the letters
printf("%c", str[i]);
i++ ;
}
}