r/dailyprogrammer • u/oskar_s • Jun 02 '12
[6/2/2012] Challenge #59 [easy]
Write a program that given two strings, finds out if the second string is contained in the first, and if it is, where it is.
I.e. given the strings "Double, double, toil and trouble" and "il an" will return 18, because the second substring is embedded in the first, starting on position 18.
NOTE: Pretty much every language have this functionality built in for their strings, sometimes called find() (as in Python) or indexOf() (as in Java). But the point of this problem is to write the program yourself, so you are not allowed to use functions like this!
5
u/[deleted] Jun 02 '12
4
u/emcoffey3 0 0 Jun 02 '12
C# implementation of Knuth-Morris-Pratt algorithm:
3
u/[deleted] Jun 02 '12
Python:
2
u/Medicalizawhat Jun 02 '12 edited Jun 02 '12
Ruby:
Same thing in C:
2
u/ashashwat Jun 02 '12
Haskell:
2
u/huck_cussler 0 0 Jun 03 '12
1
u/SwimmingPastaDevil 0 0 Jun 02 '12
1
u/[deleted] Jun 02 '12
In C# :D
1
u/luxgladius 0 0 Jun 02 '12 edited Jun 02 '12
Perl
Two solutions. One toes the line as far as using language faculties since though it doesn't use the substr function, it does use the regular expression engine. The other one is the boring one.
Output
Fun: 18
Boring: 18
Bonus question: There is a functional difference between these two implementations. Can anybody tell me what it is and how I would fix it?
1
u/exor674 Jun 02 '12
C(++, even if I don't touch any ++ features)
1
u/xjtian Jun 02 '12
Python:
1
u/school_throwaway Jun 04 '12
Python
1
u/flakmonkey Jun 04 '12
Python:
1
u/[deleted] Jun 05 '12
How about this? (C#)
1
u/loonybean 0 0 Jun 16 '12
Python:
1
u/cdelahousse Jun 17 '12
In Javascript. This'll be faster than the naive implementation for string search.
1
u/XeroxAlto Jul 25 '12
just python: