Thread: Late night part 2

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Late night part 2

    I hope this isn't a simple answer again, but my work situation doesn't allow me to code the hours i like and as often as I like. The following code emualtes C's STRSTR. however, i noticed it returns only from token[1] and up. Token being the second string argument.

    I could do some simple exception handling. But i'd prefer to know why it doesn't return the first address.
    Code:
    #include <stdio.h>
    #define FIND "s"
    
    char *MyStrstr(char *word, char *str);
    int main(void){
    
        char *word="superca9038jfcmcdldfjdf";
        char *KeyFound = MyStrstr(word,FIND);
        if (KeyFound){
         puts("found it");
         printf("Your looking for \"%s\" ",KeyFound);
     }    
         
        else
         puts("Can't find it");
          
        getchar();
        return 0;
    }
    
    
    char *MyStrstr(char *word, char *tok){
        
        while(*word++){
        if (*word == *tok)
            	return word;
         }   	
         return NULL;
     }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    while(*word++){
    I'd suggest making the loop a dowhile, as you start off the loop incrementing in to the array.

    lol, take a break man, late night coding gets to yah pretty easily.
    -edit-
    and wait, this is late night? What time zone are you in?

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    lol, take a break man, late night coding gets to yah pretty easily.
    -edit-
    and wait, this is late night? What time zone are you in?
    Thanks

    EST. Its just i work all day and when i get home its too late. The only really good time i get is in the early morning before work. then i'm usaully in the zone. I do the late night coding cause sometimes if it's not too much of a bug I can learn it, sleep on it, and have my brain work better to understand it.
    Then there are annoying simple bugs like these .....
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    lol, happens to all of us. Just something with as the night goes on, your brain slowly shuts down.

    -edit-
    and I forgot my days are still shifted forward 6 hours because of summer

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    yours is like a strchr more than an strstr. you need to keep checking past the first letter.

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by linuxdude
    yours is like a strchr more than an strstr. you need to keep checking past the first letter.
    True. And a really bad one at that. The second parameter in the protoype should be of type int not char - if it was to be a prototype for strchr. Again i was coding it late night. I'll go add that strncmp line. thanks for pointing it out. @ me
    Last edited by caroundw5h; 09-09-2004 at 10:39 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2006, 04:28 AM
  2. Can someone look over my code? part 2
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-18-2006, 06:33 AM
  3. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 07:49 AM
  4. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM