-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkklib.c
321 lines (293 loc) · 6.5 KB
/
tkklib.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Created by Ti Kyi Khant on 1/20/2023.
//
#include <stdio.h>
#define INDEXES_MAX 100
typedef struct{
int status;
int indexes[INDEXES_MAX];
int indexes_size;
} INDEXES;
int strlen_t(const char *s){
int x = 0;
for(; *s != '\0'; s++){
x++;
}
return x;
}
int strcmp_t(const char *s1, const char *s2){
if(strlen_t(s1) < strlen_t(s2)){
//printf("first string is shorter than second string\n");
return -1;
}
else if(strlen_t(s1) > strlen_t(s2)){
//printf("first string is longer than second string\n");
return -1;
}
else{
for(;*s1 == *s2; s1++,s2++){
if(*s1 == '\0' && *s2 == '\0'){
return 1;
}
}
}
return -1;
}
/*
void strcon_t(const char *s1, const char *s2, char *target){
int count = 0;
for(; (target[count] = s1[count])!='\0';count++){
;
}
for(; (target[count] = *s2) != '\0';count++,s2++){
;
}
}
*/
void strcon_t(char *s1,const char *s2){
int count = 0;
for(; *s1 != '\0'; s1++){
count++;
}
//count++;
for(; *s2 != '\0'; s2++,count++){
s1[count] = *s2;
}
}
void strcpy_t(const char *origin , char *target){
for(;(*target = *origin) != '\0'; target++,origin++){
}
}
int pow_t(const int base,int expo){
int result = 1;
if (expo == 1){
return base;
}
else if (expo == 0){
return 1;
}
else{
while(expo > 0){
result *= base;
expo--;
}
return result;
}
}
void decToBinConverter(const int num){
int x = 1 << 7;
int numcpy = num;
for(int i = 0; i < 8; i++){
if((i%4)==0){
printf(" ");
}
if((x & numcpy)==0){
printf("0");
numcpy = numcpy<<1;
}
else{
printf("1");
numcpy = numcpy<<1;
}
}
}
int binarysearch(int *arr,int z){
int arrlen_t(int *arrptr);
int middleterm(int *arrptr);
int start = 0;
int end = arrlen_t(arr)-1;
int middle = middleterm(arr);//start + (end - start)/2
while(start <= end){
if(z == arr[middle]){
return middle;
}
else if(z > arr[middle]){
start = middle + 1;
middle = start + (end - start)/2;
}
else if(z < arr[middle]){
end = middle - 1;
middle = start + (end - start)/2;
}
}
return -1;
}
int arrlen_t(int *arrptr){
int i;
for(i = 0; *arrptr != '\0';arrptr++){
i++;
}
return i-1;
}
int middleterm(int *arrptr){
int i = (arrlen_t(arrptr)/2)-1;
return i;
}
void reverse(const char *const sPtr){
if('\0' == sPtr[0]){
return;
}
else{
reverse(&sPtr[1]);
putchar(sPtr[0]);
}
}
int c2i(const char ch){
if(ch >= 48 && ch <= 57){
return ch - 48;
}
else {
return -1;
}
}
void strclear(char *str){
for(;*str != '\0'; str++){
*str = '\0';
}
}
int s2i(char *str,int count){
int res = 0;
for(;*str != '\0' && count > 0; count--,str++ ){
res += pow_t(10,count - 1) * c2i(*str);
}
return res;
}
int strchr_t(const char *str,const char ch,const int count){
int i = 0;
while(str[i] != '\0' && i < count){
if(str[i] == ch){
return i;
}
else{
i++;
}
}
return -1;
}
INDEXES find_allch(char *str,char ch,int count){
INDEXES indexes;
indexes.status = 0;
int strcounter = 0;
int fillcounter = 0;
while(str[strcounter] != '\0' && strcounter < count){
if(str[strcounter] == ch){
indexes.indexes[fillcounter++] = strcounter;
indexes.status = 1;
}
strcounter++;
}
indexes.indexes_size = fillcounter;
return indexes;
}
void lstrip_t(const char *origin, char *target,const int count){
int upperctr = 0;
int innerctr = 0;
while(origin[upperctr] == ' '){
upperctr++;
}
for(; innerctr < count - upperctr; innerctr++){
target[innerctr] = origin[upperctr+innerctr];
}
}
void rstrip_t(const char *origin, char *target,const int count){
int upperctr = strlen_t(origin) - 1;
while(origin[upperctr] == ' '){
upperctr--;
}
int innerctr;
for(innerctr = 0; innerctr <= upperctr && innerctr < count; innerctr++){
target[innerctr] = origin[innerctr];
}
target[innerctr] = '\0';
}
void strip_t(const char *origin, char *target,const int count){
int lctr = 0;
int rctr = strlen_t(origin) - 1;
int innerctr;
while(origin[lctr] == ' '){
lctr++;
}
while(origin[rctr] == ' '){
rctr--;
}
for(innerctr = 0; lctr <= rctr && innerctr < count; innerctr++,lctr++){
target[innerctr] = origin[lctr];
}
for(;innerctr < strlen_t(target); innerctr++) {
target[innerctr] = '\0';
}
}
int strncmp_t(char *first,char *second,int firstlen,int secondlen,int startindex){
if(secondlen > firstlen){
return -1;
}
for(int i = 0; i < secondlen; i++){
if(first[startindex+i] != second[i]){
return -1;
}
}
return 1;
}
INDEXES findAllstr_t(char *target,char *str){
INDEXES result,data;
int i;
int si = 0;
data.status=0;
result = find_allch(target,str[0], strlen_t(target));
if(result.status){
for(i = 0; i < result.indexes_size; i++){
int startindex = result.indexes[i];
int status = strncmp_t(target,str, strlen_t(target), strlen_t(str),startindex);
if(status==0){
data.indexes[si++] = startindex;
data.status = 1;
}
}
}
else{
data.status = 0;
}
data.indexes_size = si;
return data;
}
int strchrr_t(const char *str,const char ch,int count){
int i = count - 1;
while(str[i] != '\0'){
if(str[i] == ch){
return i;
}
else{
i++;
}
}
}
int isdigit_t(const int ch){
if(ch < 48 || ch > 57){
return -1;
}
else{
return ch - 48;
}
}
int isalpha_t(const int ch){
if((ch >= 65 && ch <= 90 )|| (ch >= 97 && ch <= 122)){
return 0;
}
else{
return -1;
}
}
char i2c(const int ch){
if((ch >= 65 && ch <= 90 )|| (ch >= 97 && ch <= 122)){
return ch;
}
else{
return -1;
}
}
int intarrlen(const int *arrptr){
int i;
for(i = 0; *arrptr != '\0'; i++,arrptr++){
}
return i;
}