C语言 strcat 在第一个字符串的开头粘贴第二个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16759688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:28:11  来源:igfitidea点击:

strcat paste second string at beginning of first string

cstringstrcat

提问by Refael

i use strcat()to connect two strings like:

strcat()用来连接两个字符串,如:

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

    int main(int argc, char *args[])
    {
       char *str1; // "456"
       char *str2; // "123"

       strcat(str1,str2);
       printf("%s",str1);
    }

i get:

我得到:

456123  

but i need the second string on beginning of first string like:

但我需要第一个字符串开头的第二个字符串,例如:

123456

how can i do it ?

我该怎么做 ?

回答by Rohan

Do strcat(str2,str1);, switch the parameters. But you will get resultant string in str2, which you can set to str1if you really want to use str1further in your program.

strcat(str2,str1);,切换参数。但是你会得到结果字符串 in str2str1如果你真的想str1在你的程序中进一步使用,你可以设置为。

However, you need to take care appropriately for memory space available in str2.

但是,您需要适当注意str2.



If you want to change str1then, do this

如果你想改变,str1那么这样做

char *tmp = strdup(str1);

strcpy(str1, str2); //Put str2 or anyother string that you want at the begining
strcat(str1, tmp);  //concatenate previous str1

...
free(tmp); //free the memory

回答by Sornii

try to use this with static sized arrays, works for me in my project.

尝试将它与静态大小的数组一起使用,在我的项目中对我有用。

void strbeg(char* strReceive, char* strInsert)
{
    int strInsertLength = strlen(strInsert);
    int strReceiveLength = strlen(strReceive);

    for(int i = strReceiveLength; i >= 0; i--)
    {
        strReceive[i + strInsertLength] = strReceive[i];
    }

    for(int i = 0; i < strInsertLength; i++)
    {
        strReceive[i] = strInsert[i];
    }
}

回答by unxnut

You need to use strcpyinstead of strcat.

您需要使用strcpy而不是strcat.

回答by alk

Verbatim from man strcat:

逐字来自man strcat

char *strcat(char *dest, const char *src);

DESCRIPTION

The strcat() function appends the src string to the dest string, overwriting the null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result.

char *strcat(char *dest, const char *src);

描述

strcat() 函数将 src 字符串附加到 dest 字符串,覆盖 dest 末尾的空字节('\0'),然后添加一个终止空字节。字符串不能重叠,dest 字符串必须有足够的空间用于结果。

As the programmer you need to make sure that the pointer char * destreferences enough valid memory to hold the addtional chars that will be copied from where char* srcpoints to.

作为程序员,您需要确保指针char * dest引用足够的有效内存来保存char将从 wherechar* src指向的附加s 。

To succesfully prefix str1with str2declare them as follows:

为了成功地前缀str1str2声明它们如下:

char str2[3 + 1] = "123"; // three for "123" plus 1 for the terminating 0
char str1[2 + 3 + 1] = "46"; // 2 for "46" plus 3 for "123" plus 1 for the terminating zero

to actually concatenate the two chararrays do so:

要实际连接两个char数组,请这样做:

strcat(str1, str2);