简体   繁体   English

C字符串串联

[英]C string concatenation

I am trying to input 2 strings in C, and output a 3rd string which the concatenation of string 1 and 2. 我试图在C中输入2个字符串,并输出第3个字符串,该字符串由字符串1和2串联而成。

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

/*
 * 
 */
int main(int argc, char** argv) {

    char stringarray1 [30];
    char stringarray2 [30];
    char stringarray3 [30];

    int length;

    printf("Please enter some text less than 30 characters long\n");
    scanf("%[a-z, ]", &stringarray1);

    printf("Please enter some text less than 30 characters long\n");
    scanf("%[a-z, ]", &stringarray2);

    strcat(stringarray1, stringarray2);


    //length = strlen(stringarray);
    printf("The combined string is %s\n", stringarray1);


    return (EXIT_SUCCESS);
}

It allows me to input the first string, but then prints the seond and third printf statement, without allowing me to enter the second string. 它允许我输入第一个字符串,但随后输出第二个和第三个printf语句,而不允许我输入第二个字符串。

How can i enter the second string without it exiting? 如何在不退出的情况下输入第二个字符串? Why does it exit? 为什么退出?

Have a look at the documentation of scanf : Your format string "%[az, ]" does not what you seem to think what it does. 看一下scanf的文档:您的格式字符串"%[az, ]"似乎与您的想法无关。 Just use "%s" for strings instead. 只需对字符串使用"%s" (It's without any character checks). (它没有任何字符检查)。

您不应将&放在已经是指针的数组中。

scanf("%[a-z, ]", stringarray2);

At a guess, I'd say you need a \\n inside your scanf specification strings. 猜测一下,我想您会在scanf规范字符串中需要一个\\n You should probably also be asking for %s instead of %[az, ] . 您可能还应该问%s而不是%[az, ] The way you have it a single character would match, then when the second scanf is hit, the next character would match. 您拥有单个字符的方式将匹配,然后在按下第二个scanf时,将匹配下一个字符。

Also there are some safety issues. 还有一些安全问题。 You should probably be checking to make sure both strings together aren't larger than 30 chars. 您可能应该检查以确保两个字符串的总和不超过30个字符。 As Benoit pointed out, arrays in C also double as pointers to the first character, so you don't use the address-of operator ( & ) on them when passing them to the C IO routines. 正如Benoit所指出的那样,C中的数组还兼用作指向第一个字符的指针,因此在将它们传递给C IO例程时,不要在它们上使用地址运算符( & )。 That would give the routine the address of your array pointer itself, which the IO routine would then merrily wipe out with character data. 这将为例程提供数组指针本身的地址,然后IO例程将轻松地将其与字符数据一起擦除。 Not good! 不好!

You know the difference between an chararray a stringarray? 您知道chararray和stringarray之间的区别吗? I think not. 我想不是。 Also you must allocate enough space for you result-string, like: 另外,您必须为结果字符串分配足够的空间,例如:

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

/*
 * 
 */
int main(int argc, char** argv) {

    char chararray1 [31];
    char chararray2 [16];
    char chararray3 [30];

    int length;

    printf("Please enter some text less than 15 characters long\n");
    scanf("%15[a-z, ]", stringarray1);

    printf("Please enter some text less than 15 characters long\n");
    scanf("%15[a-z, ]", stringarray2);

    strcat(stringarray1, stringarray2);


    //length = strlen(stringarray);
    printf("The combined string is %s\n", stringarray1);


    return (EXIT_SUCCESS);
}

I would recommend using fgets instead of scanf, that you can specify a max length (buffer target size - 1) and avoid surprises. 我建议使用fgets而不是scanf,您可以指定最大长度(缓冲区目标大小-1)并避免出现意外情况。

also the target buffer should be twice the size of your input buffers otherwise you risk a memory overwrite 目标缓冲区也应该是输入缓冲区大小的两倍,否则您将面临内存覆盖的风险

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

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