Thread: substrings

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Did I just try to fix Prelude's code ?!?
    I make mistakes occasionally, especially when I'm in a hurry or juggling several potential implementations at the same time to see which looks best. Your solution (for that part) would work, but I think if I hadn't just thrown out code off the top of my head, it would look more like this:
    Code:
    const char *substr(const char *s, const char *match)
    {
      do {
        const char *p, *q;
    
        for (p = s, q = match; *p == *q; p++, q++) {
          if (*q == '\0')
            return s;
        }
      } while (*s++ != '\0');
    
      return 0;
    }
    >i think we want to know if the next char is valid, not the actual
    What if the first character in s is '\0' and the next one is out of bounds? The inner loop will do the right thing in that case, but if you pre-increment s, the outer loop could cause a seg fault by accessing invalid memory.
    My best code is written with the delete key.

  2. #17
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Now that we are all in terms, is there any way to close this thread?

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Threads are simply abandoned when everyone has finished what they wanted to say, and the problem is solved.
    If you have a question on another topic, start a new thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    But the problem is you guys are'nt stopping posting.
    Every day I see it on the top of new posts and I can't handle the suspense

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But the problem is you guys are'nt stopping posting.
    Welcome to the public bulletin board system. Many threads diverge in unexpected and interesting ways (hopefully after the original question is answered). Because these tangents are often the only way to get a glimpse into the really advanced stuff, or nifty ways of looking at what might otherwise be boring, we actively encourage it.
    My best code is written with the delete key.

  6. #21
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking Bug Alert

    BUG ALERT !!
    Prelude,
    I found a bug (or atleast I think I have) in the substring function.
    This was the last one suggeted by you :
    Code:
    const char *substr(const char *s, const char *match){
    do {
        const char *p, *q;
    
        for (p = s, q = match; *p == *q; p++, q++) 
          if (*q == '\0')  return s;
    } while (*s++ != '\0');
    
      return 0;
    }
    BUG :
    art is a substr of artist and popart, but not of xxxartxxx, ie, substr returns true if it occurs either at the begining or the end only.
    and I changed it to this (don't ask me why. My 6th sense quickly sensed some thing in it.(I'm not jocking)):
    till now it woks fine.
    Code:
    const char *substr(const char *s, const char *match){
    do {
        const char *p, *q;
    
        for (p = s, q = match; *p == *q; p++, q++) 
          if (*(q+1) == '\0')  return s; //this line is what I changed...
    } while (*s++ != '\0');
    return 0;
    }
    and futher modified to...
    Code:
    int substr(const char *m, const char *s){
    do {
    	const char *p, *q;
    
    	for (p = s, q = m; *p == *q; p++, q++)
    	  if (*(q+1) == '\0')  return 1;
    }while (*s++ != '\0');
    
      return 0;
    }
    I'm serious about the 6th sense thingi.
    The moment I looked at it, my mind told me to tweak the if statement.
    In fact I still don't know why it ( the fix ) works!!!

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I think next time I just won't bother posting to these threads. I appreciate the bug notifications and suggestions for fixes, but keep in mind that I tossed that code together in about 2 minutes. The fix for the code was devised in less than that, all while I was being bombarded with other things. Clearly I don't have the time to come up with quality answers anymore.

    Just to salvage my pride, here's a more serious attempt:
    Code:
    #include <cstring>
    
    char *substr ( const char *s, const char *match )
    {
      if ( *match != '\0' ) {
        std::size_t len = std::strlen ( match );
    
        do {
          while ( *s != *match ) {
            if ( *s++ == '\0' )
              return 0;
          }
        } while ( std::strncmp ( s++, match, len ) != 0 );
      }
    
      return (char *)--s;
    }
    My best code is written with the delete key.

  8. #23
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    Quote Originally Posted by Prelude
    Just to salvage my pride, here's a more serious attempt:
    Code:
    #include <cstring>
    
    char *substr ( const char *s, const char *match )
    {
      if ( *match != '\0' ) {
        std::size_t len = std::strlen ( match );
    
        do {
          while ( *s != *match ) {
            if ( *s++ == '\0' )
              return 0;
          }
        } while ( std::strncmp ( s++, match, len ) != 0 );
      }
    
      return (char *)--s;
    }
    and this code returns garbage and invokes undefined behavior for most inputs if `match' points to an empty string; a possibility explicitly acknowledged by your *match != '\0' check.
    .sect signature

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but keep in mind that I tossed that code together in about 2 minutes.
    Well, Prelude, your tossed together stuff is better than my multiple hour, made from scratch, slow-bake, refrigerate, add icing stuff. But I guess when you're a perfectionist, it can bug you.

  10. #25
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and this code returns garbage and invokes undefined behavior for
    >most inputs if `match' points to an empty string
    True.

    >a possibility explicitly acknowledged by your *match != '\0' check.
    Also true. And I still made a mistake, darn it! Bah, I seem incapable of thinking properly in terms of strstr, so I'll leave it to the rest of you and go sulk in the corner.

    >But I guess when you're a perfectionist, it can bug you.
    My best code is written with the delete key.

  11. #26
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Apologising for annoying you.
    I just thought it fit to inform you. That's all.

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is there a specific reason NOT to use strstr()? Or std::string::find()?

    Do you need a way to find a substring or do you specifically want to know how to implement substring searches?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I wanted to know the working behind it, and I made one for myself( A piece of crap ). Then I wanted to know wheteher there is some easier or standard( like bubble sort for soting. ) way. Besides I don't like using predefined class es like String.

  14. #29
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by arjunajay
    Besides I don't like using predefined classes like String.
    I won't lie; I used to think the same way. It was nice being able to re-invent the wheel. However, after a while, I got tired of writing the "grunt" functions. That's when I really got into STL and the like. The ability to leverage that much code really moves the development of programs beyond the "bits and bytes" stage.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Re-inventing the wheel for educational purposes is nice. Doing it because you don't feel comfortable with predefined classes from well-tested and even standardized libraries is common, but in the end somewhat stupid. std::string and friends are part of the C++ standard, part of the C++ programming language. Without learning them, you won't ever be able to truthfully claim you really know C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, extracting substrings from a string
    By doubty in forum C Programming
    Replies: 1
    Last Post: 06-17-2009, 11:57 PM
  2. Need help on substrings and streams
    By TaiL in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2008, 06:18 PM
  3. vector of substrings
    By manav in forum C++ Programming
    Replies: 47
    Last Post: 05-10-2008, 02:05 PM
  4. Searching for a series of possible substrings inside a string
    By andrew.bolster in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 02:20 AM
  5. Searching strings - substring's
    By Vber in forum C Programming
    Replies: 4
    Last Post: 02-06-2003, 12:05 PM