Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Code Challenge Series: Substring Search

DZone's Guide to

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.

· Java Zone ·
Free Resource

Take 60 minutes to understand the Power of the Actor Model with "Designing Reactive Systems: The Role Of Actors In Distributed Architecture". Brought to you in partnership with Lightbend.

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.

Image title

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.

Learn how the Actor model provides a simple but powerful way to design and implement reactive applications that can distribute work across clusters of cores and servers. Brought to you in partnership with Lightbend.

Topics:
challenges ,code golf ,game ,search

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}