1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 char input[] = "ABCDEFGHIJklmnopqrstuvwxyz";
7 char* str = input;
8 int len = strlen(str);
9 int i = 10; //move i characters to the end of the string str.
10 int index = 0;
11 while(index < i/2) {
12 printf("%c ==> %c\n", str[index], str[i-index-1]);
13 str[index] ^= str[i-index-1];
14 str[i-index-1] ^= str[index];
15 str[index] ^= str[i-index-1];
16 index ++;
17 }
18
19 index = 0;
20 while(index < (len-i)/2) {
21 printf("%c ==> %c\n", str[i+index], str[len-index-1]);
22 str[i+index] ^= str[len-index-1];
23 str[len-index-1] ^= str[i+index];
24 str[i+index] ^= str[len-index-1];
25 index ++;
26 }
27
28 index = 0;
29 while(index < len/2) {
30 printf("%c ==> %c\n", str[index], str[len-index-1]);
31 str[index] ^= str[len-index-1];
32 str[len-index-1] ^= str[index];
33 str[index] ^= str[len-index-1];
34 index ++;
35 }
36
37 puts(str);
38 return 0;
39 }
Output
[root@localhost tmp]# gcc reverse.c && time a.out
A ==> J
B ==> I
C ==> H
D ==> G
E ==> F
k ==> z
l ==> y
m ==> x
n ==> w
o ==> v
p ==> u
q ==> t
r ==> s
J ==> k
I ==> l
H ==> m
G ==> n
F ==> o
E ==> p
D ==> q
C ==> r
B ==> s
A ==> t
z ==> u
y ==> v
x ==> w
klmnopqrstuvwxyzABCDEFGHIJ
real 0m0.001s
user 0m0.001s
sys 0m0.001s
[root@localhost tmp]#