-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrings.c
69 lines (66 loc) · 1.01 KB
/
strings.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
#include "strings.h"
int my_strlen(const char *str){
int len = 0;
while(*str++){
len++;
}
return len;
}
void my_itoa(int num, char *str){
int i = 0;
int is_negative = 0;
if(num == 0){
str[i++] = '0';
str[i] = '\0';
return;
}
if (num < 0){
is_negative = 1;
num = -num;
}
while(num != 0){
int rem = num % 10;
str[i++] = rem + '0';
num = num / 10;
}
if(is_negative){
str[i++] = '-';
}
str[i] = '\0';
int start = 0;
int end = i -1;
while(start < end){
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int my_strcmp(const char *s1, const char *s2){
while (*s1 && (*s1 == *s2)){
s1++;
s2++;
}
return (unsigned char)*s1 - (unsigned char)*s2;
}
int my_strncmp(const char *s1, const char *s2, size_t n){
while (n > 0){
if(*s1 != *s2){
return (unsigned char)*s1 - (unsigned char)*s2;
}
if (*s1 == '\0'){
return 0;
}
s1++;
s2++;
n--;
}
return 0;
}
void my_strcpy(char *dest, const char *src){
while(*src){
*dest++ = *src++;
}
*dest = '\0';
}