不用系统提供的strcat函数,自己使用字符数组编写一个函数,实现两个字符串的连接功能。
自研代码:
char* cStrcat(char destination[], char source[]); //函数声明
int iLenth(char attr[]); //函数声明
int main()
{
char cAttr[50] = "hello";
char cAttr2[] = " world";
cout << cStrcat(cAttr, cAttr2) << endl;
system("pause");
return 0;
}
int iLenth(char attr[]) { //运算字符数组长度
for (int i = 0; ; i++) {
if (attr[i] == '\0') {
return i;
break;
}
}
}
char* cStrcat(char destination[], char source[]) //合并两个字符数组
{
int destination_lenth = iLenth(destination);
int source_lenth = iLenth(source) ;
for (int i = 0; i < source_lenth; i++)
{
destination[destination_lenth + i] = source[i];
}
return destination;
}