strcat 连接 a 和 b 而不实际更改 a 或 b

strcat to concatenate a and b without actually changing a or b

我知道我可以有这样的声明

    strcat( a, b );
    int alen = strlen( a );
    printf("a and b concatenated = %s and its length is %d\n", a, alen );

但是,我想保留一个,所以我正在尝试使用更像这样的东西:

    strcat( a, b );
    int xlen = strlen( x );
    printf("a and b concatenated = %s and its length is %d\n", x, xlen );

如何使用 strcat 修复第一行,以便将 a 和 b 连接成 x?

您应该使用以下内容:-

strcpy(x,a);
strcat(x,b);
int xlen = strlen(x);
printf("a and b concatenated = %s and its length is %d\n", x, xlen );

瞧,就是这样。

我发现以下方法也有效:

    /* Concatenate a and b */
    char x[SIZE1+SIZE2] = “”;
    strcat(x , a );
    strcat(x , b );

    printf("a and b concatenated = %s and its length is %d\n", x, (int)strlen(x) );