简体   繁体   English

我的代码函数在C中使strcat函数出了什么问题?

[英]What's wrong with my code function to make strcat function in C?

#include <stdio.h>
#include <stdlib.h>

char wordsum(char FW[256],char SW[256]){
    int i;
    int j=strlen(FW);
    for (i=0;i<=strlen(SW);i++)
      FW[i+j+1]=SW[i];
    printf("%c",FW);
    return FW;
}

int main()
{
   char F[256];
   char S[256];
   printf("Enter the first word\n");
   gets(F);
   printf("Enter the Second word\n");
   gets(S);
   wordsum(F,S);
   return 0;
}

I don't know what is wrong with my code to make strcat function. 我不知道我的使strcat函数起作用的代码有什么问题。 I hope to find the answer. 我希望找到答案。

I assume that the code is written to learn more about the C language. 我认为代码编写,详细了解C语言。 If so, may I present an alternative implementation which does not use strlen(). 如果是这样,我可以提出一个不使用strlen()的替代实现。 The intention is to present some of the really nice features in the language. 其目的是提出一些非常好的功能的语言。 It may be a bit complicated to wrap ones head around the first time, but IIRC the code can be found in K&R's book The C Programming Language. 第一次绕头看可能有点复杂,但是IIRC代码可以在K&R的书The C Programming Language中找到。

Here we go: 开始了:

char* mystrcat(char *dest, const char *src)
{
    char *ret = dest;

    while (*dest)
        dest++;

    while ((*dest++ = *src++))
        ;

    return ret;
}

The first while-loop finds the end of the destination string. 第一个while循环查找目标字符串的结尾。 The second while-loop appends the source string to the destination string. 第二个while循环将源字符串附加到目标字符串。 Finally, we return a pointer to the original dest buffer. 最后,我们的指针回到原来的DEST缓冲。

The function could've been even nicer if it didn't return a pointer. 如果不返回指针,该函数本来可以更好。

void mystrcat(char *dest, const char *src)
{
    while (*dest)
        dest++;

    while ((*dest++ = *src++))
        ;
}

HTH 高温超导

There are a few problems in your function, I've changed and commented them below: 您的函数中存在一些问题,下面我对它们进行了更改和评论:

char *wordsum(char FW[256],char SW[256]){      // correct function type
   int i;

   int j=strlen(FW); 

   for (i = 0; i <= strlen(SW); i++)
       FW[i+j] = SW[i]; //change 'i + j + 1' to 'i + j'

    printf("%s",FW); //change format specifier as you are printing string not character

    return FW;
}

Then dot forget to capture the returned pointer using a char* variable in the calling function (here main() ) 然后点忘了使用调用函数中的char*变量来捕获返回的指针(此处为main()

char *result;
result = wordsum(F,S);
printf("\n%s\n", result);

Working example: https://ideone.com/ERlFPE 工作示例: https : //ideone.com/ERlFPE

There are several mistakes in your code. 您的代码中有几个错误。 They are: 他们是:

1) A function can't return an array in C and you don't need to do so. 1)函数无法在C中返回数组,因此您不需要这样做。 Change the return type from char to void of wordsum and erase the line return FW; 将返回类型从char更改为wordsum void ,并删除行return FW;

2) You want to print a string, right? 2)您想打印一个字符串,对吗? Format specifier for string is %s . 字符串的格式说明符为%s So write printf("%s",FW); 因此写出printf("%s",FW); instead of printf("%c",FW); 而不是printf("%c",FW); .

3) Do this: FW[i+j]=SW[i]; 3)这样做: FW[i+j]=SW[i]; . Why did you add an extra 1 to i+j ? 您为什么要在i+j加1? Just think logically. 只是逻辑地思考。

4) Add header file for strlen() , it's <string.h> . 4)为strlen() )添加头文件,它是<string.h>

5) Erase those asterisk marks before and after FW[i+j]=SW[i]; 5)擦除FW[i+j]=SW[i];之前和之后的那些星号FW[i+j]=SW[i]; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM