给定两个字符串,找到两个字符串共同的单词

时间:2021-01-22 14:09:47

Eg: input: char *str1 = "the are all is well"; char *str2 = "is who the"; output: common words in two given strings, return 2D array of strings.

输入:char *str1 = "the are all is well";char *str2 = "is who the";输出:两个给定字符串中的常用单词,返回字符串的2D数组。

#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;
    }

I am debugging this code in visual studios and the test is failed.Its showing this " message: Exception code: C0000005" .It means error related to memory space allocation.So where did i go wrong?

我正在visual studio中调试这段代码,但是测试失败了。它显示了“消息:异常代码:C0000005”,表示与内存空间分配相关的错误。那么我哪里做错了呢?

1 个解决方案

#1


1  

You have the statement

你有声明

output = (char **)malloc(SIZE*sizeof(char));

at two lines of your program.

在程序的两行。

You have to modify this statement in order allocate memory for the double pointer output of type char**, but you also need to allocate memory for every element of output like this :

为了为char**类型的双指针输出分配内存,您必须修改这个语句,但是您还需要为输出的每个元素分配内存,比如:

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

where x is the desired size.

其中x是期望的尺寸。

Also check for NULL pointer return, for instance

也检查空指针返回,例如

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

#1


1  

You have the statement

你有声明

output = (char **)malloc(SIZE*sizeof(char));

at two lines of your program.

在程序的两行。

You have to modify this statement in order allocate memory for the double pointer output of type char**, but you also need to allocate memory for every element of output like this :

为了为char**类型的双指针输出分配内存,您必须修改这个语句,但是您还需要为输出的每个元素分配内存,比如:

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

where x is the desired size.

其中x是期望的尺寸。

Also check for NULL pointer return, for instance

也检查空指针返回,例如

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