2016-03-21 11 views
4

Örn: input: char * str1 = "hepsi iyi"; char * str2 = "kimdir"; çıkış: verilen iki dizede ortak sözcükler, dizelerin 2B dizisini döndürür.İki dizeye verildiğinde, her iki dizede de ortak olan sözcükleri bulun.

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

Görsel stüdyolarda bu kod hata ayıklama ve test bu gösteren failed.Its "iletisi: Özel durum kodu: c0000005" .Bu nerde hata yaptım nerede bellek alanı allocation.So ilgili hata demektir?

+3

Welcome kontrol edin! [Lütfen malloc() 've ailesinin dönüş değerini C cinsinden vermemek için bu tartışmaya bakınız.] (Http://stackoverflow.com/q/605845/2173917). –

+5

Ve en okunabilir kodun ödülü ... hayır, size üzgün değil. – ckruczek

+1

Bu satırı kontrol edin 'çıktı = (char **) malloc (SIZE * sizeof (char)); ' –

cevap

1

Sen programın iki hatlarında deyimi

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

var.

O türü char** çift pointer output için bellek ayrılamadı amacıyla bu açıklamayı değiştirmek zorunda, ama aynı zamanda böyle output her eleman için bellek ayrılamadı gerekir:

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

x isteniyorsa boyut.

Ayrıca yığın taşması için örneğin

için, boş gösterici dönüş için
if (output[i] == NULL) 
    .... 
İlgili konular