简体   繁体   English

如何创建strcat的副本?

[英]How to create a copy of strcat?

I have to create a copy of some elements of the standard library in C and I have to create a copy of strcat. 我必须在C中创建标准库的某些元素的副本,并且必须创建strcat的副本。 So I have to create a function that concatenate two strings in C. I know arrays in C can't change the allocated size. 因此,我必须创建一个在C中连接两个字符串的函数。我知道C中的数组无法更改分配的大小。 The only fonction i'm allowed to use is copies i made of strlen, strstr, and write() ... My code looks like this : 我唯一可以使用的功能是由strlen,strstr和write()制成的副本...我的代码如下所示:

char    *my_strcat(char *dest, char *src)
{
    int  dest_size;
    int  src_size;
    int  current_pos;
    int  free_space;
    int  pos_in_src;

    src_size = my_strlen(src);
    dest_size = my_strlen(dest);
    while (dest[current_pos] != '\0')
        current_pos = current_pos + 1;
    free_space = dest_size - current_pos;
    if (free_space < src_size)
        return (0);
    while (src[pos_in_src] != '\0')
    {
        dest[current_pos] = src[pos_in_src];
        pos_in_src = pos_in_src + 1;
        current_pos = current_pos + 1;
    }
    return (dest);
}

But I don't know how to declare my dest and src in the main. 但是我不知道如何在主目录中声明我的dest和src。 I don't know how to create an array with a big size, declare it as a string like dest = "Hello\\0" but this array has to still contains more than 6 characters. 我不知道如何创建一个大尺寸的数组,将其声明为类似dest =“ Hello \\ 0”的字符串,但是此数组仍必须包含6个以上的字符。

Can you help me please ? 你能帮我吗 ?

char dest[19] = "epite";
char *src = "chor42spotted";

my_strcat(dest, src);

Also, read the man for strcat(3) 另外,请阅读该manstrcat(3)

the dest string must have enough space for the result. dest字符串必须有足够的空间用于结果。

https://linux.die.net/man/3/strcat https://linux.die.net/man/3/strcat

So your function is behaving incorrectly, you do not need to check that you have enough free space in dest 因此,您的功能行为不正确,您无需检查dest中是否有足够的可用空间

You want a function mystrcat which behaves exactly like stdlib strcat. 您需要一个函数mystrcat,其行为与stdlib strcat完全相同。

So the prototype is 所以原型是

 /*
    concatenate src to dest
    dest [in / out] - the string to add to (buffer must be large enough)
    src [in] - the string to concatenate.
    Returns: dest (useless little detail for historical reasons). 
 */
 char *mystrcat(char *dest, const char *src);

Now we call it like this 现在我们这样称呼它

int main(void)
{
 char buff[1024];  // nice big buffer */

 strcpy(buff, "Hello ");
 mystrcat(buff, "world");

 /* print the output to test it */
 printf("%s\n", buff);

 return 0;
} 

But I'm not going to write mystrcat for you. 但我不会为您写mystrcat。 That would make your homework exercise pointless. 那会让你的家庭作业毫无意义。

The 1st parameter of the array simply has to be large enough to contain both strings + one null terminator. 数组的第一个参数必须足够大才能包含两个字符串和一个空终止符。 So if you for example have "hello" and "world" , you need 5 + 5 +1 = 11 characters. 因此,例如,如果您有"hello""world" ,则需要5 + 5 +1 = 11个字符。 Example: 例:

#define LARGE_ENOUGH 11

int main (void)
{
  char str[LARGE_ENOUGH] = "hello";
  my_strcat(str, "world");
  puts(str); // gives "helloworld"
}

In real world applications, you would typically allocate space for the array to either be same large number (couple of hundred bytes) or with a length based on strlen calls. 在实际的应用程序中,通常会为数组分配相同的大数(数百字节)或基于strlen调用的长度。


As for the implementation itself, your solution is needlessly complicated. 至于实现本身,您的解决方案不必要地复杂。 Please note that the real strcat leaves all error checking to the caller. 请注意,真正的strcat将所有错误检查留给调用方。 It is most likely implemented like this: 它很可能是这样实现的:

char* strcat (char* restrict s1, const char* restrict s2)
{
  return strcpy(&s1[strlen(s1)], s2);
}

The most important part here is to note the const-correctness of the s2 parameter. 这里最重要的部分是要注意s2参数的const正确性。

The restrict keywords are just micro-optimizations from the C standard, that tells the compiler that it can assume that the pointers point at different memory areas. strict关键字只是C标准的微优化,它告诉编译器可以假定指针指向不同的存储区。

If you wish to roll out your own version with no library function calls just for fun, it is still rather easy, you just need two loops. 如果您想推出自己的版本而没有库函数调用只是为了好玩,那还是很容易的,您只需要两个循环。 Something like this perhaps: 大概是这样的:

char* lolcat (char* restrict s1, const char* restrict s2)
{
  char* s1_end = s1;
  while(*s1_end != '\0') // find the end of s1
  {
    s1_end++;
  }

  do // overwrite the end of s1 including null terminator
  {
    *s1_end = *s2;
    s1_end++;
    s2++;
  } while(*s1_end != '\0'); // loop until the null term from s2 is copied

  return s1;
}

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

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