8 #define MAX 100 // cheat by using wc on input file
12 int main(int argc
, char **argv
)
14 char array
[9] = "fbgdceah";
16 int limit
= strlen (array
);
18 limit
= atoi (argv
[1]);
20 freopen (argv
[2], "r", stdin
);
22 int nread
= fread (buf
, 1, sizeof buf
, stdin
);
29 } while (p
- buf
< nread
);
30 printf ("parsed %d lines, now reversing them\n", count
);
33 for (int i
= count
; i
--; ) {
34 printf ("iteration %d %s: ", i
, lines
[i
]);
35 if (!strncmp (lines
[i
], "swap", 4)) {
36 a
= sscanf (lines
[i
], "swap %c%*s %c with %*s %c", &c
, &x
, &y
);
40 array
[x
- '0'] = array
[y
- '0'];
44 for (a
= 0; a
< limit
; a
++)
47 else if (array
[a
] == y
)
50 } else if (!strncmp (lines
[i
], "rotate", 6)) {
51 a
= sscanf (lines
[i
], "rotate %c", &c
);
54 a
= sscanf (&lines
[i
][7], "%*s on position of letter %c", &c
);
56 b
= strchr (array
, c
) - array
;
58 b
= (b
& 1) ? limit
- (b
+ 1) / 2 : b
? 3 - b
/ 2 : 7;
60 a
= sscanf (&lines
[i
][7], "%*s %d s%*s", &b
);
65 for (a
= 0; a
< limit
; a
++)
66 tmp
[(a
+ b
) % limit
] = array
[a
];
67 memcpy (array
, tmp
, limit
);
68 } else if (!strncmp (lines
[i
], "reverse", 7)) {
69 c
= sscanf (lines
[i
], "reverse positions %d through %d", &a
, &b
);
71 for (c
= 0; c
< (b
- a
+ 1) / 2; c
++) {
73 array
[c
+ a
] = array
[b
- c
];
77 assert (!strncmp (lines
[i
], "move", 4));
78 c
= sscanf (lines
[i
], "move position %d to position %d", &b
, &a
);
82 memmove (array
+ a
, array
+ a
+ 1, b
- a
);
84 memmove (array
+ b
+ 1, array
+ b
, a
- b
);
87 printf ("%s\n", array
);
89 printf ("original string: %s\n", array
);