2013-10-12 46 views
1

我必須爲一個在字符串中找到一個子串的任務編寫一些代碼。找到一個子串

這裏是我的代碼,我添加了註釋:

// the target is the substring that we want to find in the source string 
// m is the length of the target, and n is the length of the source 
int contains(char target[], int m, char source[], int n) { 
int flag = 0; // the source originally does not contain the target 
int i; 

    // go through each character of the source string 
for(i = 0; i < n; i++) { 
    int targetIndex = 0; 
    int j; 

      // check if the preceding characters of the source string are a substring 
      // that matches the target string 
    for(j = i; j < n && targetIndex < m; j++) { 
     if(target[targetIndex] == source[j]) { 
      flag = 1; 
      targetIndex += 1; 
     } 
     else { 
      flag = 0; // a letter does not match 
      break; 
     } 
    } 
} 

return flag; 

}

所以,當我測試這個方法,我總是0回來,我不明白爲什麼。
如果我嘗試int i = contains("potatoes", 8, "toes", 4);它給出0
我試着把一些打印語句看看它匹配什麼字符,它似乎只發現第一個字母"t"

+1

因爲這基本上只是'strstr()',所以我建議你看看一個實現, G。一個在glibc中。 – 2013-10-12 14:52:13

+0

是否要更正此代碼?或想要更好的方式? –

回答

1

當你有一場比賽時,你需要打破外線for

你的代碼的工作方式,你可能會找到一個匹配,然後再次運行外部循環,並「忘記」它。

1

嘗試這樣的:

for(i = 0; i < n; i++) { 
    int targetIndex = 0; 
    int j; 

      // check if the preceding characters of the source string are a substring 
      // that matches the target string 
    for(j = i; j < n && targetIndex < m; j++) { 
     if(target[targetIndex] == source[j]) { 
      flag = 1; 
      targetIndex += 1; 
     } 
     else { 
      flag = 0; // a letter does not match 
      break; 
     } 
    } 
    if(flag == 1) 
    { 
    break; 
    } 
} 

可以代替使用C,這將使你的東西更容易的strstr功能嘗試。

實施例:

char *x= "Find the substring in this string"; 
char *y= "substring"; 
if(strstr(x, y) != NULL) { 
    return true; 
} 
+0

你外在'break;'無條件 –

+0

@GrijeshChauhan: - 謝謝Grijesh指出。更新了我的答案。但我認爲第二種選擇是OP更好的選擇! –

+0

現在它是好的,但實際上你不需要'else {..}'在內部循環中只在外部中斷循環之前設置'flag = 0',另外不要使用'break'而是'return i;'在外部如果。 –

0

稍加修改與說明性註釋您的代碼。

// the target is the substring that we want to find in the source string 
// m is the length of the target, and n is the length of the source 
int contains(char target[], int m, char source[], int n) { 
int flag = 0; // the source originally does not contain the target 
int i; 

    // go through each character of the source string 
for(i = 0; i < n; i++) { 
    int targetIndex = 0; 
    int j; 

      // check if the preceding characters of the source string are a substring 
      // that matches the target string 
    for(j = i; j < n && targetIndex < m; j++) { 
     if(target[targetIndex] == source[j]) { 
      targetIndex += 1; 
      if(targetIndex == m) { // the 'target' has been fully found 
       flag = 1; 
       break; 
      } 
     } 
     else 
     { 
      break; 
     } 
    } 
    if(flag == 1) // 'target' is already found, no need to search further 
    { 
     break; 
    } 
} 

return flag; 
} 

完全找到子字符串時,打破內部和外部循環。

編輯: 此外,而不是int i = contains("potatoes", 8, "toes", 4);,它應該是int i = contains("toes", 4, "potatoes", 8); - 根據您的功能描述。

相關問題