-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmy_lib.c
More file actions
100 lines (72 loc) · 1.21 KB
/
Copy pathmy_lib.c
File metadata and controls
100 lines (72 loc) · 1.21 KB
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
#include "my_lib.h"
char* my_strdup(const char* s1){
char* copy;
int tmp = 0;
int i = 0;
tmp = length((char *)s1);
copy=malloc(sizeof(char)*tmp);
while(s1[i] != '\0'){
copy[i] = s1[i];
i++;
}
copy[i]='\0';
return copy;
}
char* my_strcpy(char* dst, const char* src){
char* ptr = dst;
while ( *dst++ = *src++ );
return ptr;
}
int length(char* value){
int i = 0;
if(value == NULL)
return 0;
while(value[i] != '\0' && value[i] != '\n'){
i++;
}
return i;
}
int my_strcmp(char* array1, char* array2){
int value1 = 0;
int value2 = 0;
int i = 0;
while(array1[i] != '\0'){
value1 += (int)array1[i];
i++;
}
i = 0;
while(array2[i] != '\0'){
value2 += (int)array2[i];
i++;
}
if(value1 > value2)
return 1;
if(value1 < value2)
return -1;
if(value1 == value2)
return 0;
}
int my_atoi(char* value){
int result=0;
int current=0;
int i=0;
int sign=1;
if(value[i] == '-'){
sign = (-1);
i++;
}
while(value[i] != '\0'){
if(value[i] == 46){
result *= sign;
return result;
}
current=value[i];
if(current < 48 || current > 57){
return 0;
}
result = result*10 + value[i] - 48;
i++;
}
result *= sign;
return result;
}