重点:
1.strcat返回值为了实现链式操作,将目的地址返回,直接进行返回信息操作。
链式操作:strlen(strcat(a,b))
strcat实现
//strcat源码实现
char* strcatFunc(char* dst, const char* src)
{assert(dst != nullptr && src != nullptr);char* cp = dst;while(*cp)cp++;while (*cp++ = *src++); return dst;
}int main()
{char d[20] = "GoldenGlobal"; const char* s = "View"; strcatFunc(d, s);cout << d << endl;return 0;
}
strcpy实现:传进去的d是字符串首地址,
//strcpy源码实现
char* strcpyFunc(char* dst, const char* src)
{assert(dst != nullptr && src != nullptr);char* cp = dst;while ((*cp++ = *src++)!='\0');return dst;
}int main()
{char d[20] = "GoldenGlobal"; const char* s = "View"; int len =strlen(strcpyFunc(d, s));cout << len<<":"<<d << endl;return 0;
}
strcmp实现
//strcmp源码实现
int strcmpFunc(const char* dest, const char* source)
{/*assert((NULL != dest) && (NULL != source));*/while (*dest && *source && (*dest == *source)){dest++;source++;}return *dest - *source;
}int main()
{char d[20] = "GoldenGlobal"; const char* s = "View"; int len = strcmpFunc(d, s);cout << len<<":"<<d << endl;return 0;
}
strlen实现
//strcmp源码实现
size_t strlenFunc(const char* src)
{/*assert(src != nullptr);*/size_t len = 0;while (*src++ !='\0'){len++;}return len;
}int main()
{char d[20] = "GoldenGlobal"; int len = strlenFunc(d);cout << len<<":"<<d << endl;return 0;
}