2

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. 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;
}
11
  • 2
    Are you sure it's not returning null? Commented Jun 20, 2014 at 21:56
  • @KRUKUSA. I think i am sure about it. Commented Jun 20, 2014 at 21:57
  • @Gluttton quick guess, it the OP's implementation of strlen Commented Jun 20, 2014 at 21:58
  • ... count till '\0' on each iteration ... bad idea Commented Jun 20, 2014 at 21:59
  • 2
    Where do you expect to find the word "hello" in that sentence?? I don't see it in there at all! Commented Jun 20, 2014 at 21:59

4 Answers 4

5

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.

Try something like this:

s4 = mystrstr(s1,s2);
if(s4 != NULL)
    printf("%s\n",s4);
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that in the inner loop you add the indices i+j to access 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. This causes your access to s1[i+j] to look at the characters o, g, \0, garbage, garbage.

The benefit of C strings is that they are null terminated. So you can iterate over strings like

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

I.e. you iterate from the start of s1 until you find its terminating 0 byte. 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;

I.e. j starts at the beginning of s2, k starts where i is currently pointing at inside s1. You iterate as long as both strings are equal, and they have not reached their terminating 0 byte. If you have indeed reached the 0 byte of s2 (*j == 0), you have found the substring.

Note that you probably want to return i instead of s1, since this gives you a pointer into s1 where the requested substring starts.

1 Comment

As J0rge correctly pointed out, you stumbled across another issue with the code: Namely passing NULL to printf. However, my point above still holds and will give you nasty crashes in case you read past your string into a memory region that is protected by your CPU's memory management unit.
1
printf("%s\n",s4? s4 : "(NULL)");

1 Comment

+1. J0rge already gave one good solution that addresses the root cause. This solution allows you to print something whether or not the string is found.
0
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. Therefore, the method will return NULL. And printing NULL as a string is not possible and result in an error.

To verify this, try to replace *s1 by:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.