Thread: Haystack issue !

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What I know (or just good at guessing at) doesn't matter.

    What you need to develop are the skills to be able to write and debug your own code.
    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.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by emerald123
    So can you please rectify my flaws if possible ?
    Rectification of the flaws here means implementing a correct algorithm. You need to do it yourself. Salem's suggestion of writing simple tests is a good one: I actually implemented your program myself, with a dozen test cases, two of which revealed bugs in my implementation (with empty strings and excessively long strings that could not fit into the output buffer: but do not implement the output buffer thing until you get the basic matching right).

    Quote Originally Posted by emerald123
    Since I am unable to really figure out why I am getting incorrect results
    I already told you: your algorithm is wrong because it does not try to match the needle with the current substring of haystack from the start of the needle.

    Consider searching for "bc" in "abbc". The simple algorithm that I briefly outlined in post #8 will start by comparing 'b' from the needle with 'a' from the haystack. Not equal, so it then compares 'b' from the needle with the first 'b' from the haystack. Equal, so it goes into an inner loop and compares 'c' from the needle with the second 'b' from the haystack, saving the position of the outer loop at the first 'b' from the haystack. Not equal, so it ends the inner loop and starts over for the needle, moving to the next element in the haystack: it compares 'b' from the needle with the second 'b' from the haystack. Equal, so it goes back into the inner loop, comparing 'c' from the needle with 'c' from the haystack. At this point the end of the needle is reached, so there's a match.

    Your algorithm, on the other hand, will do this: compare 'b' from the needle with 'a' from the haystack: not equal. Compare 'c' from the needle with the first 'b' from the haystack: not equal. Compare '\0' from the needle with the second 'b' from the haystack: not equal. Access the needle out of bounds and attempt to compare with 'c' from the haystack: undefined behaviour.

    Quote Originally Posted by emerald123
    I tried above suggestions.
    If you made any changes to the code that you posted in post #11, then you should post the updated code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another big issue
    By albireo in forum C++ Programming
    Replies: 40
    Last Post: 03-22-2014, 05:24 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. new issue
    By hiddenprophecy in forum C Programming
    Replies: 3
    Last Post: 04-15-2009, 05:59 PM
  4. DNS issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-02-2008, 10:15 AM
  5. DLL issue
    By crazybucky in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2008, 12:12 PM

Tags for this Thread