2016-10-22 56 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

不错。非常简洁。 –

相关问题