2016-02-28 55 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]; 
    } 
} 

如果串具有不同的長度,規格不清楚如何琴絃結合起來。

相關問題