OT: CS education gone wrong (Was: Re: TDD is BS?)

Adam D. Ruppe destructionator at gmail.com
Sat Jun 22 07:10:31 PDT 2013


On Saturday, 22 June 2013 at 13:55:26 UTC, Jérôme M. Berger wrote:
> 	I haven't tried running it, but this looks to me like it won't 
> find "ababc" in "abababc"...


You're right. I should have went backwards all the way, not just 
in the one case.

This passes all the tests:

inout(char)* mystrstr(inout(char)* haystack, const(char*) needle) 
{
         assert(haystack !is null);

         if(needle is null)
                 return haystack;

         const(char)* where = needle;
         inout(char)* gotit;

         while(*haystack) {
                 if(*haystack == *where) {
                         if(gotit is null)
                                 gotit = haystack;
                         where++;
                         if(*where == 0)
                                 return gotit;
                 } else {
                         haystack -= where - needle;
                         where = needle;
                         gotit = null;
                 }

                 haystack++;
         }

         return null;
}


More information about the Digitalmars-d mailing list