-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.h
91 lines (67 loc) · 1.08 KB
/
string.h
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
char* itoa(int n, char* str, int radix){
char digit[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* p = str;
char* head = str;
if(!p || radix<2 || radix >36)
return p;
if(radix !=10 && n<0)
return p;
if(n==0)
{
*p++ = '0';
*p = 0;
return p;
}
if(radix ==10 && n<10)
{
*p++ ='-';
n=-n;
}
while(n)
{
*p++ = digit[ n % radix];
n /= radix;
}
*p = 0;
for(--p; head<p; ++head, --p)
{
char temp = *head;
*head = *p;
*p = temp;
}
return str;
}
int strcmp( const char* src, const char*dst){
int ret = 0;
unsigned char* p1=(unsigned char*)src;
unsigned char* p2=(unsigned char*)dst;
while(! (ret = *p1 - *p2) && *p2)
{
++p1,++p2;
}
if(ret < 0)
{
ret = -1;
}else if(ret > 0)
{
ret = 1;
}
return ret;
}
char *strcpy(char* dest, const char* src){
char* ret = dest;
while(*src)
*dest++= *src++;
*dest ='\0';
return ret;
}
unsigned strlen(const char* str){
int cnt = 0;
if(!str)
return 0;
for(;*str!='\0';++str)
{
++cnt;
}
return cnt;
}