2013-11-15 161 views
0

初級問題: 我試圖做一個字符數組,包括功能,它應該比較2個陣列 - 圖案和文字,並返回文本,類似的指標之最:比較2個字符數組失敗

int main(int argc, const char * argv[]) 
{ 

     int answer =0; 

     answer = match("viva","vviaaa"); 
     printf("%d",answer); 
     return 0; 


} 

int match(char pattern[],char text[]) { 
    int i,t,counter=0,topCount=0,topIndex=0; 
    for (t=0;pattern[t]!= '\0'; t++) { 
     counter=0; 
     for (i=t; text[i]!='\0'; i++) { 
      printf("is %c = %c ?\n",pattern[i],text[i]); 
      if (pattern[i] == text[i]) { 
       counter++; 
       printf("yes\n"); 
      } 
      if (pattern[i] =='\0') { 
       break; 
      } 
     } 
     if (counter>topCount) { 
      printf("%d > %d at index t: %d\n",counter,topCount,t); 
      topCount = counter; 
      topIndex = t; 

     } 
    } 
    return topIndex; 
} 

輸出(而非blacnk焦炭存在倒掛畫面包含「?」:

is v = v ? 
yes 
is i = v ? 
is v = i ? 
is a = a ? 
yes 
is � = a ? 
2 > 0 at index t: 0 
is i = v ? 
is v = i ? 
is a = a ? 
yes 
is � = a ? 
is v = i ? 
is a = a ? 
yes 
is � = a ? 
is a = a ? 
yes 
is � = a ? 
0Program ended with exit code: 0 
+0

你的問題是什麼? – FrankPl

+0

輸出我檢查,它是根據代碼。現在請告訴,你在做什麼?什麼是信號問題陳述?什麼是差異? – mawia

回答

2

我做了一些更改您的代碼,它現在的作品你沒有正確的比較的字符。你需要...

int match(char pattern[],char text[]); 

int main(int argc, const char * argv[]) 
{ 

    int answer = 0; 

    answer = match("viva","vviaaa"); 
    printf("%d\n",answer); 
    return 0; 
} 

int match(char pattern[],char text[]) 
{ 
    int i,j,k,counter=0,topCount=0,topIndex=0; 
    for (i=0; text[i]!= '\0'; i++) 
    { 
     counter = 0; 
     for (j=0,k=i; pattern[j]!='\0' && text[k]!='\0' && pattern[j] == text[k]; j++,k++,counter++); 

     if(counter > topCount) 
     { 
      topCount = counter; 
      topIndex = i; 
     } 
    } 
    return topIndex; 
}