2016-02-28 54 views
-1

我想逐个字母地拼接2个不同的字符串。我怎样才能做到这一点? 例如:a = "hid", b = "jof" 级联字符串应该是"hjiodf"如何拼接来自2个不同字符串的字母

到目前为止,我已经尝试过这么多:

#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; 
} 
+0

这看起来不像C++。你的意思是哪种语言? –

+0

^^也许,但有了格式,谁可以告诉? –

+0

我在这里没有看到C++。 – Joel

回答

0

既然看来你不能用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; 
} 
+0

您的建议不符合OP的目标。 – chqrlie

1

您的concatenate2并不真正进行连接,而是会覆盖。请记住,您必须展开p,才能为q中的字符提供更多空间 - 您目前还不知道。

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

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

Input 1st string: aaaaaaaa 
Input 2nd string: bb 
String obtained on concatenation is "ababaaaaaa" 

参见下面的完整代码(注意我用fgets这是输入字符串一个更安全的方式代替你的gets)。

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

void concatenate2(const char p[], const char q[], char concat[]) 
{ 
    size_t p_idx = 0, q_idx = 0; 
    size_t concat_idx = 0; 

    // Iterating through both strings. 
    while(p[p_idx] != '\0' || q[q_idx] != '\0') 
    { 
     if('\0' != p[p_idx]) 
     { 
      concat[concat_idx++] = p[p_idx++]; 
     } 

     if('\0' != q[q_idx]) 
     { 
      concat[concat_idx++] = q[q_idx++]; 
     } 
    } 

    concat[concat_idx] = '\0'; 
} 


int main(void) 
{ 
    char w[100] = "", a[100] = "", concat[200] = ""; 

    // Input first string. 
    printf("Input 1st string: "); 
    if(NULL == fgets(w, sizeof(w), stdin)) 
    { 
     perror("Invalid input"); 
     return -1; 
    } 
    w[strlen(w) - 1] = '\0'; 

    // Input second string. 
    printf("Input 2nd string: "); 
    if(NULL == fgets(a, sizeof(a), stdin)) 
    { 
     perror("Invalid input"); 
     return -1; 
    } 
    a[strlen(a) - 1] = '\0'; 

    // Concat the two strings. 
    concatenate2(w, a, concat); 

    // Print the result. 
    printf("String obtained on concatenation is \"%s\"\n", concat); 

    return 0; 
} 
+1

您的函数不会终止目标数组。 – chqrlie

+0

在'concatenate2'函数中设置空终止符比依赖于之前的初始化要可靠得多。 – chqrlie

+0

@chqrlie我看到了,增加了'concat [concat_idx] ='\ 0';'到'concatenate2' - 谢谢! – artm

1

功能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]; 
    } 
} 

如果串具有不同的长度,规格不清楚如何琴弦结合起来。

相关问题