C语言实现strcatstrlenstrcmpstrcpy

it2025-11-07  9

主要考虑两点:

返回值对使用的便利性。

边界,null的判断。

strcat

char *m_strcat(char *des, const char *src) { assert((des != NULL) && (src != NULL)); char *add = des; while (*des != '\0') ++des; while (*des++ = *src++) ; return add; }

strlen

int m_strlen(const char *str) { assert(str != NULL); int len = 0; while (*str != '\0') { ++str; ++len; } return len; }

strcmp

int m_strcmp(const char *des, const char *src) { // return 0 des==src , + des>src , - des<src assert((des != NULL) && (src != NULL)); while ((*des) == (*src)) { if (*des == '\0') return 0; ++des; ++src; } return *des - *src; }

strcpy

char *m_strcpy(char *des, const char *src) { assert((des != NULL) && (src != NULL)); char *add = des; while ((*des++ = *src++) != '\0') ; return add; }

转载于:https://www.cnblogs.com/ruoh3kou/p/11230546.html

相关资源:数据结构—成绩单生成器
最新回复(0)