2016-10-22 57 views
0

我需要創建一個函數來連接2個字符串,在我的情況下它們已經被給出。我需要連接字符串'hello'和'world!'把它變成'helloworld!'。但是,除了strlen()之外,我不能使用庫函數。我也需要使用malloc。我知道malloc會創建n個字節的內存,但是,我怎樣才能使它返回一個字符串數組,如果可能的話。如何連接使用malloc而不是庫函數的2個字符串

這裏是我到目前爲止,

#include <stdio.h> 
#include <string.h> 
int *my_strcat(const char* const str1, const char *const str2) 
{ 
    int s1, s2, s3, i = 0; 
    char *a; 

    s1 = strlen(str1); 
    s2 = strlen(str2); 
    s3 = s1 + s2 + 1; 

    a = char *malloc(size_t s3); 

    for(i = 0; i < s1; i++) 
     a[i] = str1[i]; 

    for(i = 0; i < s2; i++) 
     a[i+s1] = str2[i]; 

    a[i]='\0'; 

    return a; 
} 

int main(void) 
{ 
    printf("%s\n",my_strcat("Hello","world!")); 
    return 0; 
} 

由於任何人誰可以幫助我。

+2

返回int *是錯誤的 –

+0

'strlen()'返回'size_t' * not *'int'。 – alk

回答

0

有幾個問題:

在從malloc回你不需要做任何CAST(你曾經爲投錯了反正語法)(見this瞭解更多信息)。

您需要爲malloc函數包含標頭stdlib.h

而最重要的是,a[i]='\0';在這i是不是你所需要的;你想在末尾添加空字符,該字符應該是a[s3]='\0';(s1 + s2的長度)。

這個版本應該是正確的(除非我錯過了什麼):

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

char *my_strcat(const char* const str1, const char *const str2) 
{ 
    int s1,s2,s3,i=0; 
    char *a; 
    s1 = strlen(str1); 
    s2 = strlen(str2); 
    s3 = s1+s2+1; 
    a = malloc(s3); 
    for(i = 0; i < s1; i++) { 
     a[i] = str1[i]; 
    } 
    for(i = 0; i < s2; i++) { 
     a[i+s1] = str2[i]; 
    } 
    a[s3-1] = '\0'; // you need the size of s1 + s2 + 1 here, but - 1 as it is 0-indexed 

    return a; 
} 


int main(void) 
{ 
    printf("%s\n",my_strcat("Hello","world!")); 
    return 0;  
} 

測試與Ideone呈現此輸出:Helloworld!

+1

返回類型不應該是'char *'而不是'int *'嗎? –

+0

@SandeepTuniki哦對,錯過了。我認爲你是正確的(儘管在這種情況下可能並不重要,因爲char可能是一個無符號整數,但我認爲它是實現定義的) – jpw

+0

哦,我明白了。我實際上在理解如何使用malloc tbh時遇到了一些麻煩。非常感謝! – joeymed

3

這裏是一個備用的修復。首先,您忘記了#include <stdlib.h>malloc()。您從函數my_strcat()返回指向char的指針,因此您需要更改函數原型以反映此情況。我也改變了const聲明,以使該指針不是const,只有它們指向的值:

char * my_strcat(const char *str1, const char *str2); 

您對malloc()呼叫被錯誤地投,並有no reason to do so anyway in C。它也看起來像你試圖在malloc()size_t投下參數。你可以這樣做,但你必須圍繞與括號中的類型標識符:

a = malloc((size_t) s3); 

相反,我已經改變了類型聲明s1, s2, s3, isize_t因爲所有這些變量的字符串長度和數組的上下文中使用指數。

這個循環是最重要的變化,也是我改變函數原型中的const的原因。你的循環看起來不錯,但你也可以使用指針。您通過遞增指針來逐步完成字符串,遞增計數器i,並將存儲在那裏的值存儲在a的第i位置。最後,索引i已經遞增,以指示一個位置超過最後一個字符,並且在那裏存儲'\ 0'。請注意,在您的原始代碼中,計數器i未遞增以指示連接字符串的空終止符的位置,因爲您在通過str2循環時重置該位置。 @jpw顯示瞭解決這個問題的一種方法。

我改變了main()只是一點。我聲明瞭一個指向char的指針,以接收函數調用的返回值。這樣你可以在你使用它時分配內存。

下面是修改代碼:

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

char * my_strcat(const char *str1, const char *str2) 
{ 
    size_t s1, s2, s3, i = 0; 
    char *a; 

    s1 = strlen(str1); 
    s2 = strlen(str2); 
    s3 = s1+s2+1; 
    a = malloc(s3); 

    while(*str1 != '\0') { 
     a[i] = *str1; 
     str1++; 
     i++; 
    } 
    while(*str2 != '\0') { 
     a[i] = *str2; 
     str2++; 
     i++; 
    } 

    a[i] = '\0';     // Here i = s1 + s2 

    return a; 
} 


int main(void) 
{ 
    char *str = my_strcat("Hello", "world!"); 
    printf("%s\n", str); 

    /* Always free allocated memory! */ 
    free(str); 

    return 0; 
} 
+1

'sizeof(char)'總是不必要的,也是一個壞主意。 –

+0

@ ChrisDodd--你是對的。我試圖對OP進行清晰的教學,並且似乎對'malloc()'有一些麻煩,但它確實沒有幫助。我在我的答案中解決了這個問題。 –

+0

將所有's's設置爲'size_t',並且在將其傳遞給'malloc()'時,您可以將醜陋的類型轉換爲's3'。 – alk

2

,該問題是指針海事組織簡單一些:

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

char *mystrcat(char *a, char *b) { 
    char *p, *q, *rtn; 
    rtn = q = malloc(strlen(a) + strlen(b) + 1); 
    for (p = a; (*q = *p) != '\0'; ++p, ++q) {} 
    for (p = b; (*q = *p) != '\0'; ++p, ++q) {} 
    return rtn; 
} 

int main(void) { 
    char *rtn = mystrcat("Hello ", "world!"); 
    printf("Returned: %s\n", rtn); 
    free(rtn); 
    return 0; 
} 

但是你可以用指數同樣的事情:

char *mystrcat(char *a, char *b) { 
    char *rtn = malloc(strlen(a) + strlen(b) + 1); 
    int p, q = 0; 
    for (p = 0; (rtn[q] = a[p]) != '\0'; ++p, ++q) {} 
    for (p = 0; (rtn[q] = b[p]) != '\0'; ++p, ++q) {} 
    return rtn; 
} 
+0

不錯。非常簡潔。 –

相關問題