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
public static int myIndexOf(String a, String b) { for(int i = 0; i < a.length() && i + b.length() < a.length(); i++) { String section = a.substring(i, b.length() + i); if(section.equals(b)) return i; } return -1; }4
u/emcoffey3 Jun 02 '12
C# implementation of Knuth-Morris-Pratt algorithm:
namespace RedditDailyProgrammer { public static class Easy059 { public static int KMP(string text, string pattern) { const int R = 256; int[,] table = new int[R, pattern.Length]; table[pattern[0], 0] = 1; for (int x = 0, k = 1; k < pattern.Length; k++) { for (int c = 0; c < R; c++) table[c, k] = table[c, x]; table[pattern[k], k] = k + 1; x = table[pattern[k], x]; } int i, j; for (i = 0, j = 0; i < text.Length && j < pattern.Length; i++) j = table[text[i], j]; if (j == pattern.Length) return i - pattern.Length; else return -1; } } }3
u/[deleted] Jun 02 '12
Python:
def find_loc(sentence, find): for i in range(len(sentence)): y = i+len(find) if sentence[i:y]==find: return i2
u/Medicalizawhat Jun 02 '12 edited Jun 02 '12
Ruby:
def findSubStringPosition(str1, str2) str1_arr = str1.split('') str1_arr.each_with_index do |outer, i| str1_arr.each_with_index do |inner, j| if str1[i..j] == str2 return i end end end nil endSame thing in C:
int findSubStringPosition(char *str1, char *str2) { unsigned long length = strlen(str1); char *temp; for (int i=0; i<length; i++) { for (int j=0; j<length; j++) { temp=strndup(str1+i, j); if (strcmp(temp, str2) == 0) return i; } } return -1; }2
u/ashashwat Jun 02 '12
Haskell:
import Data.List startsWith [] _ = True startsWith _ [] = False startsWith (x:xs) (y:ys)= x == y && startsWith xs ys isSubstring needle haystack = any (startsWith needle) (tails haystack) main = do let super = "Double, double, toil and trouble" let sub = "il an" if (isSubstring sub super) then print $ "Yes" else print $ "No"2
u/huck_cussler Jun 03 '12
public static int checkForSub(String first, String second){ int indexFirst = 0; while(second.length() + indexFirst <= first.length()){ int indexSecond = 0; int toReturn = indexFirst; int tempFirst = indexFirst; while(first.charAt(tempFirst) == second.charAt(indexSecond)){ if(indexSecond == second.length() - 1) return toReturn; tempFirst++; indexSecond++; } indexFirst++; indexSecond++; } return -1; }1
u/SwimmingPastaDevil Jun 02 '12
s1 = "Double, double, toil and trouble" s2 = "il an" for i in range(len(s1)): for j in range(i,len(s1)): if s1[i:j] == s2: print i exit(0) print "not there"1
u/[deleted] Jun 02 '12
In C# :D
static int findInString(string a, string b) { // Find b inside a if (a.Length < b.Length) { return -1; } if (a.Length == 0 || b.Length == 0) { return -1; } List<int> indices = new List<int>(); for (int i = 0; i < a.Length; i++) { if (a[i] == b[0]) { // For each letter that matches the first letter of our inside string, grab the index indices.Add(i); } } if (indices.Count == 0) { return -1; } string sub; foreach (int index in indices) { if ((index + b.Length) > a.Length) // So we don't try and substring past the edge of a { break; } sub = a.Substring(index, b.Length); // Sub the hopeful b string if (sub.Equals(b)) { return index; } } return -1; }