标签:
unsigned long myStrlen(char string[])
{
unsigned long length = 0;
while (string[length] != ‘\0‘) {
length++;
}
return length;
}
void myStrcpy(char string1[], char string2[])
{
int i = 0;
while (string2[i] != ‘\0‘) {
string1[i] = string2[i];
i++;
}
string1[i] = ‘\0‘;
}
void myStrcat(char string1[], char string2[])
{
//找string1的‘\0‘位置
int i = 0;
while (string1[i] != ‘\0‘) {
i++;
}
//把string2加到string1后面
int j = 0;
while (string2[j] != ‘\0‘) {
string1[i++] = string2[j++];
}
//不要忘记在最后添加\0
string1[i] = ‘\0‘;
}
int myStrcmp(char string1[], char string2[])
{
int i = 0;
while (string1[i] == string2[i] && string1[i] != ‘\0‘) {
i++;
}
return string1[i] - string2[i];
}
自定义方法实现strcpy,strlen, strcat, strcmp函数,了解及实现原理
标签:
原文地址:http://www.cnblogs.com/tian-sun/p/4307519.html