对于C标准库中的字符串处理函数应该平常用的比较多;简单实现strcat和strcmp
_strcpy:
1 char *_strcpy(char *dest, char *src) 2 { 3 char *buf = dest; 4 while((*dest++ = *src++)) 5 ; 6 return buf; 7 }
_strcmp:
1 int _strcmp(char *str1, char *str2) 2 { 3 if(str1 == NULL || str2 == NULL) 4 return -1; 5 while(*str1 == *str2) 6 { 7 if(*str1 == ‘\0‘) // *str1 == 0 8 return 0; 9 str1++; 10 str2++; 11 } 12 return *str1 - *str2; 13 }
_strlen:
1 int _strlen(char *str) 2 { 3 int i= 0; 4 while(*str) 5 { 6 i++; 7 str++; 8 } 9 return i; 10 }
_strcat:
1 char *_strcat(char *dest, char *src) 2 { 3 if(sizeof(dest) - _strlen(dest) < _strlen(src)) 4 { 5 printf("!!alert : not enough space of dest for src !\n"); 6 return dest; 7 } 8 char *buf = dest; 9 while(*dest) 10 dest++; 11 _strcpy(dest, src); 12 return buf; 13 }
Source Code : https://github.com/huaixzk/_strs.so
C语言简单strcat和strcmp的实现,布布扣,bubuko.com
时间: 2024-10-10 14:43:28