基本上我要做的就是重新制作我自己的 c-string。strlen、strcmp、strcpy 和 strcat。下面的代码在我的头文件中:
int mystrlen(const char pcString[]) //strlen function
{
const char *pcStringEnd = pcString;
while (*pcStringEnd != '\0')
pcStringEnd++;
return pcStringEnd - pcString;
}
int mystrcmp(char *s1, char *s2) // strcmp function
{
while(*s1 == *s2)
{
if(*s1 == '\0' || *s2 == '\0')
break;
first++;
second++;
}
if(*first == '\0' && *second == '\0')
return (0);
else
return (-1);
}
char mystrcpy(char *s1, const char *s2) // strcpy function
{
while(*s2)
{
*s1 = *s2;
s2++;
s1++;
}
*s1 = '\0';
}
char mystrcat(char *s1, const char *s2) //strcat function
{
char *string1 = s1;
const char *string2 = s2;
char *catString = string1 + string2;
return catString;
}
大多数错误是未定义的标识符,但问题是我不能不更改 main.cpp 中的内容。只能修改头文件。我会把我的 main.cpp 放在这里,但它的代码很长。
{
char *string1 = s1;
const char *string2 = s2;
char *catString = string1 + string2; //There is an error here with string 2 and catring.
return catString;
}