简体   繁体   English

查找字符串中的子字符串

[英]Find substring in a string

Here is my code to find substring entered by the user in the given string. 这是我的代码,用于查找用户在给定字符串中输入的子字符串。

bool find_str(char *str, char const *substr) {
    while(*str) {
        if(*str++ == *substr) {
            char const *a = substr;
            while((*str++ == *++a));  /*empty*/
            if(*a == '\0')
                return true;
        }
    }
    return false;
}
// If match found, then return true, else false

int main(void) {
  printf("%d", find_str("ABCDEF", "CDE"));  /* Return true in this case */
  printf("%d", find_str("ABCDE", "CDE")); /* Return false in this case */

}

As explained in the comment, it returns true whenever it ends with an additional characters. 如注释中所述,只要以其他字符结尾,它将返回true。 If it is not, then it returns false. 如果不是,则返回false。 I think there is a problem in increment/decrement operator. 我认为增量/减量运算符有问题。 But I could not find how? 但是我找不到如何?

This is because your code decides to stop on finding \\0 only after performing the comparison 这是因为您的代码决定仅执行比较后才停止查找\\0

*str++ == *++a

This condition will be true even when the match happens at the end of the string on null terminators, so the while loop would happily proceed beyond the end of both strings past the null terminator, causing undefined behavior. 即使匹配发生在null终止符上的字符串末尾,此条件也将true ,因此while循环将很高兴地在两个字符串的末尾经过null终止符,从而导致不确定的行为。

Changing the condition to exit when *a is zero should fix the problem: 更改*a为零时退出的条件应该可以解决此问题:

while((*str++ == *++a) && (*a));

I analysed you piece of code a little bit and based on my analysis, I think the problem is in here 我对您的代码进行了一些分析,根据我的分析,我认为问题出在这里

        while((*str++ == *++a));    /*empty*/

perhaps you would like to add another statement like below 也许您想添加如下另一条语句

while((*str++ == *++a) && ( *a != '\0' ) ) ;    /*empty*/

I guess you are missing a null check, what if both pointer are pointing to NULL termination they will still go forward that's exactly whats happening 我猜您缺少一个空检查,如果两个指针都指向NULL终止,它们仍将继续前进,这正是发生的情况

I was going through your piece of code and found quite a few interesting things 我正在检查您的代码段,发现了很多有趣的东西

  1. Lets say the memory allocated for CDE is at X 假设分配给CDE的内存为X
  2. say again the memory allocated for ABCDEF is at X+4 (this was the case in my machine) 再说一次分配给ABCDEF的内存为X + 4(我的机器就是这种情况)
  3. and say memory block allocated for ABCDE is at some X+Y or what ever 并说分配给ABCDE的内存块是X + Y或其他

now when the function is called second time both pointers a and str point to respective memory locations starting at X+2 where Character C satisfies the condition above, however the condition will still be true even if they reach to end ie at X+3 and hence a will move forward and point to A which makes your program behave erroneously 现在,当第二次调用该函数时,指针a和str都指向从字符C满足上述条件的X + 2开始的相应存储位置,但是,即使它们到达结尾(即在X + 3和因此a会向前移动并指向A,这会使您的程序行为错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM