tags:

views:

124

answers:

3

I have made strstr() function but the program does not give any output,just a blank screen.Please have a look at the code.

#include<stdio.h>
#include<conio.h>

    const char* mystrstr(const char *str1, const char *str2);
    int main()
    {
        const char *str1="chal bhai nikal";
        const char *str2="nikal",*result;
        result=mystrstr(str1,str2);
        printf("found at %d location",(int*)*result);
        getch();
        return 0;


    }
    const char * mystrstr(const char *s1, const char *s2)
    {
        int i,j,k,len2,count=0;
        char *p;
        for(len2=0;*s2!='\0';len2++);//len2 becomes the length of s2
        for(i=0,count=0;*s1!='\0';i++)
        {
            if(*(s1+i)==*s2)
            {
                for(j=i,k=0;*s2!='\0';j++,k++)
                {
                    if(*(s1+j)==*(s2+i))
                    count++;
                    if(count==len2)
                    {
                        p=(char*)malloc(sizeof(char*));
                        *p='i';
                    return p;
                    }
                }

            }

        }
        return NULL;
    }
+4  A: 

The line with this comment:

//len2 becomes the length of s2

is broken. You repeatedly check the first character of s2. Instead of *s2, try s2[len2].

Edit: as others have said, there are apparently a lot more things wrong with this implementation. If you want the naive, brute-force strstr algorithm, here's a concise and fast version of it:

char *naive_strstr(const char *h, const char *n)
{
    size_t i;
    for (i=0; n[i] && h[i]; i++)
       for (; n[i] != h[i]; h++) i=0;
    return n[i] ? 0 : (char *)h;
}
R..
My aim was to check it against the first character of s2.Actually i thought that if the first character matches,i will run a loop to compare the string from that position
fahad
That is the line in which i have measured the length of the second string.
fahad
That line is an infinite loop, as written. (Unless `s2` is the empty string.)
R..
Ahh got it .Thanks a lot.
fahad
+3  A: 

It looks like this is an exercise you're doing to learn more about algorithms and C strings and pointers, so I won't solve those issues for you, but here are some starting points:

  • You have an infinite loop when calculating len2 (your loop condition is *s2 but you're never changing s2)
  • You have a similar issue with the second for loop, although I you have an early return so it might not be infinite, but I doubt the condition is correct
  • Given you want to behave like strstr(), you should return a pointer to the first string, not a new pointer you allocate. There is no reason for you to allocate during a function like strstr.
  • In main() if you want to calculate the position of the found string, you want to print result-str1 unless result is NULL). (int*)*result makes no sense - result should be a pointer to the string (or NULL)
Matt Curtis
A: 

You also need to change this line:

if(*(s1+j)==*(s2+i))

to this:

if(*(s1+j)==*(s2+k))

As already mentioned, the return value is a bit odd. You are returning a char* but kind of trying to put an integer value in it. The result doesn't make sense logically. You should either return a pointer to the location where it is found (no malloc needed) or return the integer position (i). But returning the integer position is not the "typical" strstr implementation.

Mark Wilkins