c语言常用字符串函数实现
1.str_cmp函数实现
View Code
1 int str_cmp(const char *src, const char *dst)
2 {
3 int ret = 0;
4 while (!(ret = *(unsigned char*)src) - *(unsigned char*)dst && *dst)
5 {
6 ++src;
7 ++dst;
8 }
9
10 if (ret < 0)
11 {
12 ret = -1;
13 }
14 else if (ret > 0)
15 {
16 ret = 1;
17 }
18
19 return (ret);
20 }
2.str_cpy函数实现
View Code
1 char *str_cpy(char *dst, const char *src)
2 {
3 char *address;
4
5 assert((NULL != dst) && (NULL != src));
6
7 address = dst;
8
9 while ((*dst++ = *src++) != '\0') {
10 NULL;
11 }
12
13 return address;
14 }
3.str_cat函数实现
View Code
1 char *str_cat(char *dst, const char *src)
2 {
3 char * cp = dst;
4
5 while( *cp )
6 cp++;
7
8 while(*cp++ = *src++) ;
9
10 return( dst );
11 }
4.str_len函数实现
View Code
1 size_t str_len(const char * str)
2 {
3 const char * eos = str;
4 while( *eos++ );
5 return (eos - str - 1);
6 }
5.str_rev函数实现
View Code
1 char * str_rev(char * string)
2 {
3 char *start = string;
4 char *left = string;
5 char ch;
6
7 while (*string++)
8 ;
9 string -= 2;
10
11 while (left < string)
12 {
13 ch = *left;
14 *left++ = *string;
15 *string-- = ch;
16 }
17
18 return (start);
19 }
6.str_str函数实现