简体   繁体   English

在字符串中查找子字符串

[英]Finding substring in a string

I am writing a function to find substring. 我正在编写一个函数来查找子字符串。 But I am not sure where i am going wrong. 但我不知道我哪里出错了。 On running GDB i get a segmentation fault. 在运行GDB时,我遇到了分段错误。 If someone can guide me in the right direction. 如果有人可以指导我正确的方向。

here is the code 这是代码

char *mystrstr(char * s1, const char * s2)



int main(){
    char *s1 = "The quick brown fox jumps over the hell lazy dog";
    char *s2 = "hello";
    char *s4;
    s4 = mystrstr(s1,s2);
    printf("%s\n",s4);  <--- this is where i am Seg. Faulting



    return 0;
}

When s2 is not a substring of s1 you are returning null, and then you are trying to print it, that gives a segmentation fault. 当s2不是s1的子字符串时,您返回null,然后您尝试打印它,这会产生分段错误。

Try something like this: 尝试这样的事情:

s4 = mystrstr(s1,s2);
if(s4 != NULL)
    printf("%s\n",s4);

The problem is that in the inner loop you add the indices i+j to access s1 . 问题是在内部循环中添加索引i+j以访问s1 If you imagine i to point to the "o" in "dog" in your example, j goes from 0 to 5 (length of "hello") in the inner loop. 如果你想象i在你的例子中指向“dog”中的“o”, j在内循环中从0变为5(长度为“hello”)。 This causes your access to s1[i+j] to look at the characters o , g , \\0 , garbage , garbage . 这使您访问s1[i+j]来查看字符og\\0垃圾垃圾

The benefit of C strings is that they are null terminated. C字符串的好处是它们是空终止的。 So you can iterate over strings like 所以你可以迭代字符串

for (char* i = s1; *i != 0; i++) {
    ...
}

Ie you iterate from the start of s1 until you find its terminating 0 byte. 即你从s1的开始迭代,直到找到它的终止0字节。 In your inner loop, this allows you to write the following: 在内部循环中,这允许您编写以下内容:

const char *j, *k;
for (j = s2, k = i; *j == *k && *j != 0; j++, k++);
if (*j == 0)
    return i;

Ie j starts at the beginning of s2 , k starts where i is currently pointing at inside s1 . 即, j开始于s2的开始, k开始于i当前指向s1内部的位置。 You iterate as long as both strings are equal, and they have not reached their terminating 0 byte. 只要两个字符串相等,它们就会迭代,并且它们没有到达终止的0字节。 If you have indeed reached the 0 byte of s2 ( *j == 0 ), you have found the substring. 如果你确实达到了s2的0字节( *j == 0 ),你就找到了子串。

Note that you probably want to return i instead of s1 , since this gives you a pointer into s1 where the requested substring starts. 请注意,您可能希望返回i而不是s1 ,因为这会为您提供指向所请求的子字符串开始的s1的指针。

printf("%s\n",s4? s4 : "(NULL)");
char *s1 = "The quick brown fox jumps over the hell lazy dog";
char *s2 = "hello";

According to mystrstr *s2 is not substring of *s1 because you cannot find hello in the *s1 . 根据mystrstr *s2不是*s1子串,因为你在*s1找不到hello Therefore, the method will return NULL . 因此,该方法将返回NULL And printing NULL as a string is not possible and result in an error. 并且无法将NULL作为字符串打印并导致错误。

To verify this, try to replace *s1 by: 要验证这一点,请尝试将*s1替换为:

char *s1 = "The quick brown fox jumps over the hello lazy dog"; // replace hell by hello

The output will be: 输出将是:

The quick brown fox jumps over the hello lazy dog

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

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