strpos error

Discussion in 'C' started by CrazyGal, May 30, 2009.

  1. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Hi there,

    Could someone tell me why this aint working? I understand this could be done using strstr() function. I just gave it a try and I cant quite figure out why this piece of code
    returns NULL though the search string is present in the main string.

    Please let me know.

    Thanks,
    Tina
    Code:
    #include <stdio.h>
    
    char * strpos(char *, char *);
    
    int main()
    {
        char s[] = "I love my job";
        char t[] = "job";
        
        char *pos = strpos(s, t);
    
        printf("%s", pos);
    
        return 0;
    }
    
    char * strpos(char *a, char *b)
    {
        char *pa;
        char *pb;
    
        for(; *a; a++ )
           for(pa = a, pb = b; *pb && (*pa == *pb); pa++, pb++ )
               if(*pb == '\0')
                   return a;
        return NULL;
    
    }
     
    Last edited by a moderator: May 31, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If *pb is NULL, do you think "*pb && (*pa == *pb)" will evaluate TRUE or FALSE?

    BTW, please use code blocks when posting code. It preserves the formatting and makes it much more readable.
     
  3. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    It will evaluate to False. Yea, I figured out my mistake. I changed *pb == '\0' to pb = (b + strlen(b) -1) and it worked.

    I'll start using code blocks from my next post. Sorry about that!

    Thanks!
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    I don't believe it ! How can it work ?
    You should change it to pb == (b + strlen(b) -1) not pb = (b + strlen(b) -1)
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    In my fix I just removed pb from the test, i.e.
    Code:
    for(pa = a, pb = b; (*pa == *pb); pa++, pb++ )
    
    Edit: Oops! This only works if t is at the end of s. This works; tested with s="abcjobxyz" and t="job"
    Code:
    		while (*pb && (*pa == *pb))
    		{
    			if(pb == (b + strlen(b) -1))
    				return a;
    
    Edit2: SaswatPadhi's right, you ALWAYS need to be suspicious of a single = in an expression of any type. = and == are NOT the same. if (a=b) is valid; it assigns b to a then evaluates TRUE or FALSE depending on the value of both, so it's useful in stuff like while (x=somefunc()) where somefunc returns zero when it's finished.
     
    Last edited: May 31, 2009
  6. CrazyGal

    CrazyGal New Member

    Joined:
    May 24, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Oops! That's a typo. I meant '==' and that's precisely I've in my code :)

    Thanks,
    Tina
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice