-
Notifications
You must be signed in to change notification settings - Fork 4
/
loop-without-length.cpp
139 lines (131 loc) · 2.61 KB
/
loop-without-length.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// https://github.com/BaseMax/CheckReverseString
#include <stdio.h>
#define swapItem(firstItem,secondItem) \
firstItem^=secondItem;\
secondItem^=firstItem;\
firstItem^=secondItem;
char *reverse0(char* input) {
char* firstItem = input;
char *lastItem = input;
char tempItem;
while(*lastItem)
lastItem++;
while(lastItem-- > firstItem) {
tempItem = *firstItem;
*firstItem++ = *lastItem;
*lastItem = tempItem;
}
return input;
}
void reverse1(char *input) {
// TODO: Check 0xF, 0xE, 0xC, 0xD
char *temp = input;
while(temp && *temp) // != NULL
temp++;
temp--;
while(input < temp) {
*input = *input ^ *temp;
*temp = *input ^ *temp;
*input = *input ^ *temp;
input++;
temp--;
}
}
char* reverse2(char* input) {
char tempItem;
char* firstItem = input;
char* lastItem = input;
while(*lastItem)
lastItem++;
while(lastItem > firstItem) {
lastItem--;
tempItem = *firstItem;
*firstItem++ = *lastItem;
*lastItem = tempItem;
}
return input;
}
char* reverse3(char* input) {
char tempItem;
char* firstItem = input;
char* lastItem = input;
while(*lastItem)
lastItem++;
while(lastItem-- > firstItem) {
tempItem = *firstItem;
*firstItem++ = *lastItem;
*lastItem = tempItem;
}
return input;
}
void reverse4(char *input) {
char *temp = input;
while(temp && *temp) // != NULL
temp++;
for(temp--; input < temp; input++, temp--) {
*input = *input ^ *temp;
*temp = *input ^ *temp;
*input = *input ^ *temp;
}
}
void reverse5(char *input) {
char *temp = input;
while(temp && *temp) // != NULL
temp++;
for(temp--; input < temp; input++, temp--) {
swapItem(*input, *temp);
}
}
void reverse6(char *input) {
char *tempItem = input;
reverse5(input);
while(tempItem && *tempItem)
++tempItem;
while(input < --tempItem) {
char character=(*tempItem & 0xF0) >> 4;
switch(character) {
//four bytes
case 0xF:
{
swapItem(*(tempItem-0), *(tempItem-3));
swapItem(*(tempItem-1), *(tempItem-2));
tempItem -= 3;
}
break;
// 3 bytes
case 0xE:
{
swapItem(*(tempItem-0), *(tempItem-2));
tempItem -= 2;
}
break;
// 2 bytes
case 0xC:
case 0xD:
{
swapItem(*(tempItem-0), *(tempItem-1));
tempItem--;
}
break;
}
}
}
int main() {
// char input1[20]="Hey! سلام";
char input1[20]="Hey!";
printf("%s\n", input1);
reverse1(input1);
reverse2(input1);
reverse3(input1);
reverse4(input1);
reverse5(input1);
reverse0(input1);
printf("%s\n", input1);
////////////////////////////
char input2[20]="Hey! سلام";
printf("%s\n", input2);
reverse6(input2);
printf("%s\n", input2);
////////////////////////////
return 0;
}