字符串连接而不使用c中的库函数?

时间:2014-04-12 15:41:55

标签: c arrays concatenation

我正在编写代码来模拟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); 
    }
}

1 个答案:

答案 0 :(得分:1)

您必须返回string1 strcat

return string1;