/
strrev.c
60 lines (56 loc) · 1.36 KB
/
strrev.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
#include <stdio.h>
static void swap(char *p, char *q)
{
char tmp = *p;
*p = *q, *q = tmp;
}
void strrev(char *p)
{
char *q = p;
if (NULL==q) return;
while (*q) ++q; /*move to tail*/
for (--q/*skip '\0'*/;q>p;++p,--q) swap(p, q);
}
void strrev_r(char *p, int lo, int hi)
{
if (lo >= hi) return;
char c = p[lo];
p[lo] = p[hi], p[hi] = c;
strrev_r(p, lo+1, hi-1);
}
void strrev_utf8(char *p)
{
char *q = p;
if (NULL==q) return;
strrev(p); /*base case*/
/*now try to fix in case there is utf-8 codes*/
while (*q) q++; /*move to tail, aka. '\0'*/
while (--q > p) {
switch((*q & 0xf0)>>4) {
case 0xc: case 0xd: /*2-byte, 110xxxxx 10xxxxxx*/
swap(q, q-1), q-=1;
break;
case 0xe: /*3-byte, 1110xxxx 10xxxxxx 10xxxxxx */
swap(q, q-2), q-=2;
break;
case 0xf: /*4-byte, 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx*/
swap(q, q-3), swap(q-1, q-2), q-=3;
break;
}
}
}
int main(void)
{
char s[128], i, len;
while (scanf("%s", s)!=EOF) {
for (i = 0, len = 0; s[i]; i+=1, len+=1)
;
printf("in : %s\n", s);
strrev(s);
//strrev_utf8(s);
printf("out: %s\n", s);
strrev_r(s, 0, len-1);
printf("out: %s\n", s);
}
return 0;
}