-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.c
107 lines (81 loc) · 1.35 KB
/
string.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
char* itoa(int n, char* str, int radix){
char digit[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* p = str;
char* head = str;
int neg = 0; //judge negative flag ==> '-'
if(!p || radix<2 || radix >36)
return p;
if(radix !=10 && n<0)
{
// -|n| handle only radix=10
return p;
}
if(n==0)
{
*p++ = '0';
*p = 0;
return p;
}
if(radix ==10 && n<0)
{
*p++ ='-';
n=-n;
neg =1 ;
}
while(n)
{
*p++ = digit[ n % radix];
n /= radix;
}
*p = 0;
if(neg){
head++ ; //skip '-' negtive symbol
}
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;
}
void memset(char* buf, int value, int len){
int i=0;
for(i=0; i<len; i++){
buf[i] = value;
}
}