Code Challenge Series: Substring Search
Find the first instance of an arbitrary length substring within a string without using regex or the find/indexOf/search functions.
Join the DZone community and get the full member experience.Join For Free
Last week we had Fizz Buzz code golf and the winner was Mark Ogden in Perl with an amazing 63 characters.
There were a couple of answers that were also very close to that as well so well done to them too.
This weeks challenge is a little harder and should really tax your brains. Basically I would like you to find the first instance of an arbitrary length substring within a string without using regular expressions or the find/indexOf/search functions of the String object. So writing it in a low level aproach at a character level.
So in the case of the above example I am looking for the index of the first letter of the word end in the string you see there.
Specification/Rules of problem:
Find the index of the first occurrence of an arbitrary length substring within a string.
Use what ever language you prefer.
Must be done at the character/int/byte level.
No Regex or String functions may be used apart from charAt/getBytes/getCharArray etc equivalent functions ie functions for retrieving the characters in whatever form.
For this challenge there will be two winners, one for the shortest implementation in characters and one for the most elegant/efficient solution in my opinion.
Good luck and the main thing is: have fun.
Opinions expressed by DZone contributors are their own.