在C中追加字符串

时间:2012-10-08 05:52:19

标签: c string

如何组合多个字符串。 例如,

char item[32];
scanf("%s", item);
printf("Shopping list: %s\n", item); //I want to combine this string 
char string_2[] = "To do list: Sleep\n"; // with this string 
char hw[32];
scanf("%s", hw); 
printf("Homework: %s\n", hw); // and this string

所以他们将打印如下,

购物清单:(项目)

待办事项列表:睡眠

家庭作业:( hw)


但我不想像上面的代码那样单独给出printf命令,而是组合字符串并在末尾调用printf /


我怎么能这样做,因为我无法将这样的内容保存到单独的字符串char string1 = ("Shopping list: %s \n", item)

6 个答案:

答案 0 :(得分:2)

连接字符串,最好是strcat函数。

但请确保您连接的目标字符串足够大,以适应所有内容(包括终止'\0'字符)。

答案 1 :(得分:2)

使用strcpystrcat

char item[] = "Shopping list";
char hw[] = "To do list: Sleep \n";
char* itemhw;
itemhw = malloc(strlen(item)+strlen(hw)+1);
strcpy(itemhw, item);
strcat(itemhw, hw); 
free(itemhw);

答案 2 :(得分:1)

您可以使用sprintf

char combinedstring[100];
sprintf(combinedstring,"%s %s %s",item,string_2,hw);

您还可以查找string.h标题及其功能。

答案 3 :(得分:1)

在C中处理原始字符串总是很难看。您必须执行两个步骤来连​​接字符串:

  1. 为新字符串分配内存。

  2. 将源字符串复制到新缓冲区中。

  3. 作为一个教训,请注意这个问题已经有三个不同的答案三个不同的缓冲区溢出错误。

    这是一个连接两个字符串并返回一个新字符串的函数:

    char *astrcat(const char *x, const char *y)
    {
        size_t nx = strlen(x), ny = strlen(y);
        char *z = malloc(nx + ny + 1);
        if (!z)
            return NULL;
        memcpy(z, x, nx);
        memcpy(z + nx, y, ny + 1);
        return z;
    }
    

    你可以扩展它以使用三个字符串,或者你可以调用它两次。 Sane人倾向于使用库或大量使用固定大小的缓冲区。

    备选方案:对于固定大小的缓冲区和snprintf,您可以获得安全性和易用性,但是您仍然坚持使用固定大小的缓冲区。

    const char *x = ..., *y = ...;
    char buf[100];
    snprintf(buf, sizeof(buf), "%s%s", x, y);
    

    如果您没有snprintf,那么您可能正在使用MSVC,其_snprintf几乎相同,但并不总是终止输出。

    请勿使用strcatstrcpy极少数情况下strcat可以接受:

    char buf[100];
    strcpy(buf, x); // buffer overflow
    strcat(buf, y); // buffer overflow
    

    请勿使用sprintf它也会溢出:

    char buf[100];
    sprintf(buf, "%s%s", x, y); // buffer overflow
    

答案 4 :(得分:1)

我要忽略多个问题(输入和输出的排序;输入的错误处理;输入上的缓冲区溢出;单个单词与条目中的多个单词等),但是:

char item[32];
scanf("%s", item);
char hw[32];
scanf("%s", hw);
char string_2[] = "To do list: Sleep\n";

printf("Shopping list: %s\n%sHomework: %s\n", item, string_2, hw);

这为您提供了一个printf()语句,提供了连接输出。显然,串联在输出文件(标准输出)上,而不是直接在内存中。如果你想在内存中连接字符串,那么你必须经历一些阴谋以确保有足够的内存来复制,但是你可以使用snprintf()来代替printf()

char item[32];
scanf("%s", item);
char hw[32];
scanf("%s", hw);
char string_2[] = "To do list: Sleep\n";

// This length does account for two extra newlines and a trailing null
size_t totlen = strlen(item) + strlen(homework) + sizeof(string_2) +
                sizeof("Shopping list: ") + sizeof("Homework: ");
char *conc_string = malloc(totlen);

snprintf(conc_string, totlen, "Shopping list: %s\n%sHomework: %s\n",
         item, string_2, hw);

答案 5 :(得分:0)

考虑使用伟大但未知的open_memstream()函数。

FILE *open_memstream(char **ptr, size_t *sizeloc);

使用示例:

// open the stream
FILE *stream;
char *buf;
size_t len;
stream = open_memstream(&buf, &len);

// write what you want with fprintf()
char item[32];
scanf("%s", item);
fprintf(stream, "Shopping list: %s\n", item);
char string_2[] = "To do list: Sleep\n";
fprintf(stream, "%s\n", string_2);
char hw[32];
scanf("%s", hw); 
fprintf(stream, "Homework: %s\n", hw);

// close the stream, the buffer is allocated and the size is set !
fclose(stream);
printf ("the result is '%s' (%d characters)\n", buf, len);
free(buf);

让内核管理缓冲区分配,它比你做得更好!