-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
92 lines (83 loc) · 1.44 KB
/
helper.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
#include "my_lib.h"
void my_putchar(char c)
{
write(1, &c, 1);
}
int find_index(char* string, char number)
{
int index;
index = 0;
while(string[index] != '\0')
{
if(number == string[index])
{
return index;
}
index++;
}
return -1;
}
char* my_strcpy(char* dst, char* str)
{
int index;
index = 0;
while (str[index] != '\0')
{
dst[index] = str[index];
index++;
}
dst[index] = '\0';
return dst;
}
// copy without malloc
int my_strcmp(char* a, char* b)
{
int count;
count = 0;
while (a[count] != '\0' && b[count] != '\0' && a[count] == b[count])
count++;
return (a[count] - b[count]);
}
int my_strlen(char* str)
{
int index;
index = 0;
while (str[index] != '\0')
{
index += 1;
}
return index;
}
// copy with malloc
char* my_strdup(char* str)
{
int index;
int length;
char* my_str;
length = my_strlen(str);
my_str = (char*)malloc(sizeof(char) * length + 1);
if (!my_str)
{
return 0;
}
index = 0;
while (str[index] != '\0')
{
my_str[index] = str[index];
index += 1;
}
my_str[index] = '\0';
return my_str;
}
int is_digits(char* str)
{
int index = 0;
while (str[index] != '\0') {
if(str[index] < '0' || str[index] > '9')
{
return 1;
}
index++;
}
return 0;
}