给定两个字符串,找到这两个字符串共同拥有的单词。

4

例如:输入:char *str1 = "the are all is well"; char *str2 = "is who the"; 输出:这两个给定字符串中的公共单词,返回一个二维字符串数组。

#define SIZE 31

char ** commonWords(char *str1, char *str2) {

    int i,j=0,count1,count2,k=0,a,b,m=0,n;
    char str3[100][100], str4[100][100];
    char **output;
    output = (char **)malloc(SIZE*sizeof(char*));
    if (str1 == NULL || str2 == NULL)
    {
        return NULL;
    }
    for (i = 0; str1[i] != '\0'; i++)
    {
        if (str1[i] != ' ')
        {
            str3[j][k++] = str1[i];
        }
        else
        {
            str3[j][k++] = '\0';
            j++;
            k = 0;
        }
    }
    str3[j][k++] = '\0';
    count1 = j > 0 ? j + 1 : j;
    j =  k = 0;
    for (i = 0; str2[i] != '\0'; i++)
    {
        if (str2[i] != ' ')
        {
            str4[j][k++] = str2[i];
        }
        else
        {
            str4[j][k++] = '\0';
            j++;
            k = 0;
        }
    }
    str4[j][k++] = '\0';
    count2 = j > 0 ? j + 1 : j;
    for (i = 0; i < count1; i++)
    {
        for (j = 0; j < count2; j++)
        {
            if (str3[i][k] == str4[j][k])
            {
                if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2] == '\0')
                {
                    a = i;
                    b = k;
                    while (str3[a][b] != '\0')
                    {
                        output = (char **)malloc(SIZE*sizeof(char));
                        output[m][n] = str3[a][b];
                        n++;
                        b++;
                    }
                    output[m][n] = '\0';
                }
                else if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2])
                {
                    a = i;
                    b = k;
                    while (str3[a][b] != '\0')
                    {
                        output = (char **)malloc(SIZE*sizeof(char));
                        output[m][n] = str3[a][b];
                        n++;
                        b++;
                    }
                    output[m][n] = '\0';
                    m++;
                }
            }
        }
    }
    return output;
    }

我正在Visual Studios中调试此代码,但测试失败了。它显示了这个消息:"message: Exception code: C0000005"。这意味着与内存空间分配有关的错误。那么我错在哪里了?

3
欢迎来到 Stack Overflow!请查看这篇讨论,了解为什么不要在 C 语言中将 malloc() 等函数的返回值强制转换。(链接:https://dev59.com/dHRB5IYBdhLWcg3wgHWr) - Sourav Ghosh
5
最易读代码奖颁给了……不,抱歉不是给你。 - ckruczek
1
请检查这行代码:output = (char **)malloc(SIZE*sizeof(char)); - Sourav Ghosh
1
为什么你不断用新的分配覆盖“output”?这样会丢失先前分配的内存。 - M Oehm
1
如果output是一个char **,那么它是一个包含char *值的数组。因此,字节数量应该是SIZE*sizeof(char *),而不是你所写的SIZE*sizeof(char)。你只分配了你要使用的内存的一部分。 - Tom Karzes
1个回答

1
你有这个声明。
output = (char **)malloc(SIZE*sizeof(char));

在您的程序中有两行代码。

您需要修改这个语句以便为类型为char**的双指针output分配内存,但您还需要为每个output元素分配内存,如下所示:

int i;
output = (char **)malloc(SIZE*sizeof(char*));
for (i = 0; i < SIZE; i++)
    output[i] = (char *)malloc(x*sizeof(char));

其中x为所需大小。

同时检查空指针返回,例如

if (output[i] == NULL)
    ....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接