C語言字串結合功能

本帖最後由 haloPaul 於 2018-12-22 12:35 編輯

在C語言裏面,有一個字串結合功能strcat(str1, str2);可以調用

但是,我想靠自己寫一個一模一樣的功能出來,而不用strcat(str1, str2);這個內部提供的功能。
以下的程式碼是我寫的,但是運行結果不對。請問如何實現這個字串結合功能?

#include <stdio.h>
int main() {

char dataA[30]; //first data
char dataB[30]; //second data
char finalData[60]; //concate data

int i = 0;

printf("Please enter a text: ");
while (1) {

scanf("%s", &dataA); //read in 1st data
scanf("%s", &dataB); //read in 2nd data

finalData[60] = dataA[30] + dataB[30];

printf("The link of text is %s ", finalData);
}
return 0;
}

本帖最後由 EITCo 於 2018-12-22 17:16 編輯

你可能寫慣其他更高階的語言,有得就咁 cat = str_a + str_b
C相對較低階,無咁直覺,資料的格式同運算都更貼近硬件層面
C的string其實只係char[],所以concat string其實即係concat array

如果擺脫唔到既有 str_a + str_b 的諗法
可以想像依家係做緊concat int[]
  1. int a[6] = { 0, 10, 20 };
  2. int b[] = { 30, 40, 50 };
  3. int a_len = 3, b_len = 3, i;
  4. for (i = 0; i < b_len; i++) {
  5.     a[a_len + i] = b[i];
  6. }
複製代碼
strcat(dest, src) 係將src駁係dest後面
以上都先寫左將b駁係a後面的版本

然後轉返做char[]
另外string有最尾的 '\0' 要注意
而string長度如果唔用strlen,亦係靠數 '\0' 係第幾個char
  1. char *my_strcat(char *dest, const char *src) {
  2.     // Find length of dest
  3.     // Can also use strlen(dest)
  4.     int dest_len = 0;
  5.     while (dest[dest_len] != '\0') {
  6.         dest_len++;
  7.     }
  8.     // At this point, dest[dest_len] == '\0'
  9.     // It will be overriden by src[0] in the below loop
  10.     int i;
  11.     for (i = 0; src[i] != '\0'; i++) {
  12.         dest[dest_len + i] = src[i];
  13.     }

  14.     // Put null char at the new end of dest
  15.     dest[dest_len + i] = '\0';
  16.     return dest;
  17. }
複製代碼
如果了解pointer,其實用pointer取代array[index]的寫法仲簡潔
另外以下省略左啲 != '\0'
  1. char *my_strcat_2(char *dest, const char *src) {
  2.     char *char_p = dest;

  3.     while (*char_p) {
  4.         char_p++;
  5.     }
  6.     while (*src) {
  7.         *char_p++ = *src++;
  8.     }
  9.     *char_p = '\0';

  10.     return dest;
  11. }
複製代碼
最後如果要唔改動輸入的兩條strings,另造一條新的,就要用malloc
而上面第7至10行需要用兩次,所以寫個function (其實即係strcpy)
  1. char *my_strcpy(char *dest, const char* src) {
  2.     while (*src) {
  3.         *dest++ = *src++;
  4.     }
  5.     *dest = '\0';
  6.     return dest;
  7. }
  8. char *my_alloc_strcat(char *head, char *tail) {
  9.     int head_len = strlen(head);
  10.     char *concat = malloc(sizeof(char) * (head_len + strlen(tail) + 1));

  11.     my_strcpy(concat, head);
  12.     my_strcpy(concat + head_len, tail);

  13.     return concat;
  14. }
複製代碼
啊仲有
char dataA[30]; scanf("%s", &dataA);
應該無 & 先岩,因為 dataA 已經係一個地址

有別於以下要 &i, &c 先係地址
int i; scanf("%d", &i);
char c; scanf("%c" &c);

TOP

補埋示範
  1. void print_triple(char *str_0, char *str_1, char *str_2) {
  2.     printf(""%s"\n"%s"\n"%s"\n\n", str_0, str_1, str_2);
  3. }
  4. int main() {
  5.     char a[4 + 3 + 1] = "abcd"; // + 1 for '\0'
  6.     char b[] = "efg";
  7.     char *c = my_strcat(a, b);
  8.     print_triple(a, b, c);
  9.     // "abcdefg"
  10.     // "efg"
  11.     // "abcdefg"

  12.     char d[] = "hijk";
  13.     char e[] = "lmn";
  14.     char *f = my_alloc_strcat(d, e);
  15.     print_triple(d, e, f);
  16.     // "hijk"
  17.     // "lmn"
  18.     // "hijklmn"

  19.     return 0;
  20. }
複製代碼

TOP

本帖最後由 haloPaul 於 2018-12-22 19:03 編輯
補埋示範
EITCo 發表於 2018-12-22 17:31



    唔該晒EITCo您呀,解釋得好詳細,而家明白左好多。
    平常我最驚就係POINTER,感覺C語言真係博大精深。

    但係我想問printf(""%s"\n"%s"\n"%s"\n\n", str_0, str_1, str_2);第二行在compiler編譯不到?

    PS:我是用DEV C++

TOP

差左啲escape啫
printf("\"%s\"\n\"%s\"\n\"%s\"\n\n", str_0, str_1, str_2);
我本來有打,被論壇個代碼排版功能過濾左

TOP

本帖最後由 java2 於 2018-12-24 10:47 編輯

你學好C pointer 再問, 這是C 的基本功

在C語言裏面,有一個字串結合功能strcat(str1, str2);可以調用

但是,我想靠自己寫一個一模一樣的功能出來 ...
haloPaul 發表於 2018-12-22 12:33

TOP

不要教新手在function 內malloc 又唔free 返, 咁好易搞出memory leakage.

最後如果要唔改動輸入的兩條strings,另造一條新的,就要用malloc
而上面第7至10行需要用兩次,所以寫個function (其實即係strcpy)

TOP

在C語言裏面,有一個字串結合功能strcat(str1, str2);可以調用

但是,我想靠自己寫一個一模一樣的功能出來 ...
haloPaul 發表於 2018-12-22 12:33



    其實 sample code 嚟, 唔少 C 書都有, 再唔係搵呢本 https://en.wikipedia.org/wiki/The_C_Programming_Language

    或者去 glibc 睇下人地點寫.

https://github.com/lattera/glibc/blob/master/string/strcat.c

TOP

如果齋係搞 strcat,可以試吓咁:-
  1. char cCat[100];
  2.   sprintf(cCat,"%s%s",dataA,dataB);
  3.   printf("String after concatenation: %s\n", cCat);
複製代碼

TOP

如果齋係搞 strcat,可以試吓咁:-
cal22cal 發表於 2018-12-24 18:15



    唔係話唔畀咁寫, 但係 sprintf 內部其實復雜過 strcat 好多.
如果係為咗練習 array 同 string operation , 又何需殺雞用牛刀??
strlen , strcat , strcpy 都係簡單到一個點.

TOP