【发布时间】:2014-05-26 18:51:41
【问题描述】:
我正在编写代码来模拟 c 库中的 strcat 函数,但我无法通过 main 中的第一个测试。我想知道是否有人可以指导我为什么。这是我的代码:
#include <stdio.h>
char *strcat(char string1[ ], char string2[ ])
{
int i;
int j;
for(i = 0; string1[i] != '\0'; i++);
for(j=0;string2[j] != '\0';j++)
{
string1[i] = string2[j];
i++;
}
string1[i] = '\0';
}
int main() {
char str1[81], str2[81];
char again = 'y', newline;
while (again == 'y') {
printf("Enter a string\n");
scanf("%s", str1);
printf("Enter another string\n");
scanf("%s", str2);
printf("The concatention is %s\n", strcat(str1, str2));
printf("Second test: The concatenation is %s\n", str1);
printf("The second string is still %s\n", str2);
printf("Again? (y/n)\n");
scanf("%c%c", &newline, &again);
}
}
【问题讨论】:
-
退货声明将是一个很好的起点......
-
请在C99 with Technical corrigenda TC1, TC2, and TC3 included 中查看
strcpy()的合同。
标签: c arrays concatenation