【问题标题】:Finding a substring寻找子串
【发布时间】:2013-10-12 14:41:59
【问题描述】:

我必须为在字符串中找到子字符串的赋值编写一些代码。

这是我的代码,我添加了 cmets:

// 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"

【问题讨论】:

  • 因为这基本上只是strstr(),我建议你看看一个实现,例如。 G。 glibc 中的那个。
  • 您要更正此代码吗?还是想要更好的方法?

标签: c substring


【解决方案1】:

当你有匹配时,你需要打破外部for

你的代码的工作方式,你可能会找到一个匹配,然后再次运行外循环并“忘记”它。

【讨论】:

    【解决方案2】:

    试试这样:

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

    【讨论】:

    • 你外break;是无条件的
    • @GrijeshChauhan:- 感谢 Grijesh 指出这一点。更新了我的答案。但我认为OP必须采用的第二种选择更好!
    • 现在很好,但实际上你不需要在内部 for 循环中使用else{..},只需在外部中断循环之前设置flag = 0,另外不要在外部使用break 而不是return i;如果。
    • 还有一点不需要额外的标志变量,只需检查j &gt; i + m是否在外部for循环中...实际上OP编写的代码可以改进很多。
    【解决方案3】:

    使用解释性 cmets 对您的代码进行一些修改。

    // 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("toes", 4, "potatoes", 8); 而不是 int i = contains("potatoes", 8, "toes", 4); - 根据您的功能描述。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2019-07-20
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      相关资源
      最近更新 更多