Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何从两个不同的字符串中逐个字母连接_C - Fatal编程技术网

C 如何从两个不同的字符串中逐个字母连接

C 如何从两个不同的字符串中逐个字母连接,c,C,我想一个字母一个字母地连接两个不同的字符串。我该怎么做? 例如:a=“hid”,b=“jof” 连接的字符串应该是“hjiodf” 到目前为止,我已经尝试了很多: #include <stdio.h> #include <conio.h> void concatenate2(char p[], char q[]) { int c = 0, d = 0; //Iterating through both strings while (p[c] !=

我想一个字母一个字母地连接两个不同的字符串。我该怎么做? 例如:
a=“hid”,b=“jof”
连接的字符串应该是
“hjiodf”

到目前为止,我已经尝试了很多:

#include <stdio.h>
#include <conio.h>

void concatenate2(char p[], char q[]) {
    int c = 0, d = 0;
    //Iterating through both strings
    while (p[c] != '\0' || q[d] != '\0' ) {
        //Increment first string and assign the value      
        c++;
        p[c] = q[d];
        //Increment second string and assign the value    
        d++;
        p[c] = q[d];
    }
}  //<<====== missing }

int main(void)
{
    char w[100], a[100];
    //input first string 
    printf("Input a string\n");
    gets(w);
    //input second string 
    printf("Input Second string\n");
    gets(a);
    //function call      
    concatenate2(w, a);
    //print result   
    printf("String obtained on concatenation is \"%s\"\n", w);
    getch();
    return 0;
}
#包括
#包括
无效连接2(字符p[],字符q[]{
int c=0,d=0;
//遍历两个字符串
而(p[c]!='\0'| q[d]!='\0'){
//递增第一个字符串并赋值
C++;
p[c]=q[d];
//递增第二个字符串并赋值
d++;
p[c]=q[d];
}

}//既然您似乎不能使用C的strcat,而且我不认为您必须使用数组,我建议您使用指针,例如:

#include <stdio.h>
#include <stdlib.h> // for malloc/free

int concatenate (char *buffer, int buffsize, const char *s1, const char *s2) {
    int npos = 0;
    // concatenate s1 to buffer
    while (*s1 != '\0') {
        *buffer = *s1;
        buffer++;
        s1++;
        ++npos;
    }
    // concatenate s2 to buffer
    while (*s2 != '\0') {
        *buffer = *s2;
        buffer++;
        s2++;
        ++npos;
    }
    buffer = '\0';
    return npos;
}

int main () {
    const int buff_path = 64;
    char *buffer;
    int ret;
    buffer = (char*)malloc (sizeof(char)*buff_path);
    ret = concatenate (buffer, buff_path, "hello", " world");
    printf ("This is the string: [%s]\nstring len: %i\n", buffer, ret);
    free (buffer);
    return 0;
}
Input 1st string: aaaaaaaa
Input 2nd string: bb
String obtained on concatenation is "ababaaaaaa"
#包括
#包括//用于malloc/免费
int串联(字符*缓冲区、int buffsize、常量字符*s1、常量字符*s2){
int npos=0;
//将s1连接到缓冲区
而(*s1!='\0'){
*缓冲区=*s1;
缓冲区++;
s1++;
++非营利组织;
}
//将s2连接到缓冲区
而(*s2!='\0'){
*缓冲区=*s2;
缓冲区++;
s2++;
++非营利组织;
}
缓冲区='\0';
返回非营利组织;
}
int main(){
常量int buff_path=64;
字符*缓冲区;
int ret;
缓冲区=(char*)malloc(sizeof(char)*buff_路径);
ret=连接(缓冲区,buff_路径,“hello”,“world”);
printf(“这是字符串:[%s]\n字符串长度:%i\n”,buffer,ret);
自由(缓冲);
返回0;
}

您的
连接2
实际上并不进行连接,而是进行覆盖。请记住,您必须展开
p
,才能为
q
中的字符留出更多的空间,而您目前没有这样做

chqrlie已经提供了一个优雅的解决方案。此解决方案允许您使用准确的原型进行
连接2
。此解决方案的唯一限制是两个字符串的大小必须相同

这里我提供了一个不同的解决方案。其思想是使用额外的字符串进行连接。它要求您传入一个额外的
concat
字符串。此解决方案允许连接不同大小的字符串,例如:

#include <stdio.h>
#include <stdlib.h> // for malloc/free

int concatenate (char *buffer, int buffsize, const char *s1, const char *s2) {
    int npos = 0;
    // concatenate s1 to buffer
    while (*s1 != '\0') {
        *buffer = *s1;
        buffer++;
        s1++;
        ++npos;
    }
    // concatenate s2 to buffer
    while (*s2 != '\0') {
        *buffer = *s2;
        buffer++;
        s2++;
        ++npos;
    }
    buffer = '\0';
    return npos;
}

int main () {
    const int buff_path = 64;
    char *buffer;
    int ret;
    buffer = (char*)malloc (sizeof(char)*buff_path);
    ret = concatenate (buffer, buff_path, "hello", " world");
    printf ("This is the string: [%s]\nstring len: %i\n", buffer, ret);
    free (buffer);
    return 0;
}
Input 1st string: aaaaaaaa
Input 2nd string: bb
String obtained on concatenation is "ababaaaaaa"
请参阅下面的完整代码(注意,我用
fgets
替换了
gets
,这是输入字符串更安全的方法)

#包括
#包括
#包括
无效连接2(常量字符p[],常量字符q[],字符concat[]
{
大小p_idx=0,q_idx=0;
尺寸=0;
//遍历两个字符串。
而(p[p|idx]!='\0'| q[q|idx]!='\0')
{
如果('\0'!=p[p_idx])
{
concat[concat_idx++]=p[p_uidx++];
}
如果('\0'!=q[q_idx])
{
concat[concat_idx++]=q[q_idx++];
}
}
concat[concat_idx]='\0';
}
内部主(空)
{
字符w[100]=“”,a[100]=“”,concat[200]=“”;
//输入第一个字符串。
printf(“输入第一个字符串:”);
if(NULL==fgets(w,sizeof(w),stdin))
{
perror(“无效输入”);
返回-1;
}
w[strlen(w)-1]='\0';
//输入第二个字符串。
printf(“输入第二个字符串:”);
if(NULL==fgets(a,sizeof(a),stdin))
{
perror(“无效输入”);
返回-1;
}
a[strlen(a)-1]='\0';
//把两条线连在一起。
连接2(w,a,concat);
//打印结果。
printf(“连接时获得的字符串是\%s\”\n,concat);
返回0;
}

函数
concatenate2
无法按写入方式工作,因为它在使用目标缓冲区的字符之前会覆盖目标缓冲区

按以下方式进行修改:

void concatenate2(char p[], const char q[]) {
    int i, len = strlen(p);
    p[len + len] = '\0';
    for (i = len; i-- > 0;) {
        p[i + i + 1] = q[i];
        p[i + i] = p[i];
    }
}

如果字符串长度不同,则对于组合字符串的规范不清楚。

这看起来不象C++。你指的是哪种语言?^^^也许,但有了这种格式,谁能告诉我?我看不到C++。“DILT工作”只会让你失望,关闭你的函数。你的函数不会终止目标数组。在Nave> CuthATEATE 2<代码>函数中设置空终止符比依赖先前的初始化更可靠。@ ChqrLee,我补充,<代码> CONTAT [ CopATTIIX] =‘0’;<代码>连接到
连接2
-谢谢!对的这样,您就可以在
main
中取消初始化
concat
。您的提案不符合OP的目标。