C 自己实现strcpy,strcmp,strlen,strcat等函数
// mystrlen() 测试字符长度方法 int mystrlen(char *str) { int cnt = 0;
char *p= str; while(*p++ != '\0') { cnt++; } return cnt; } //mystrcpy() 字符串拷贝 char *mystrcpy(char *dest, char *src) { char *p = dest; while((*src != '\0')&&(*p++ = *src++)); *p = '\0'; return dest; } //mystrcat() 字符串拼接 char *mystrcat(char *dest, char *src) { char *p = dest; while(*p != '\0') //找到目的地的末尾 { p++; } while(*src != '\0') { *p = *src; p++; src++; } *p = '\0'; return dest; } // mystrcmp() 字符串比较 int mystrcmp(char *dest, char *src) { while((*dest != '\0') && (*src != '\0') && (*dest == *src)) { dest++; src++; } return (*dest-*src); }
// 字符串长度测试方法 int strlen(char *str) { char *s = str; while(*s++); return (s-str-1); }