0

I am writing my own function, who checks if the second string is contained within the first string. I know this method is already implemented, but I could not find its source code and I need it for my exam.

private static bool Contains(String s1, String s2) { //s1 = bababi, s2= babi
        int correctCharCount = 0;         
        for(int i = 0, j = 0; i < s1.Length; i++) {        
            if (s1[i] == s2[j]) {
                correctCharCount++;
                j++;
            } else {
                correctCharCount = 0;                   
                j = 0;
                i--;
            }               
        }
        Console.WriteLine("count: " + correctCharCount);
        Console.WriteLine("s2 length: " + s2.Length);
        if (correctCharCount == s2.Length) return true;
        else return false;
}

My problem is, that the first three chars from the second string are the same as the first three chars from the first string. The 4th char is different. Now I want to go back to the 3rd char from s1 and start from here again with my 1st char from s2, but i get into a loop.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
flubix
  • 49
  • 5
  • 2
    [Cheat code](https://referencesource.microsoft.com/#mscorlib/system/string.cs,2172) ;) – Sinatr Jan 10 '19 at 12:39
  • 1
    If you aren't allowed to cheat, then [here](https://stackoverflow.com/a/20673608/1997232) is something similar, notice nested loop, you'll need it too. – Sinatr Jan 10 '19 at 12:42
  • im not allowed to cheat or to use any other implemented methods, but the second links works for me. thanks! – flubix Jan 10 '19 at 13:06

1 Answers1

3

Try this (necessary comments are in code):

public static bool Contains(string stringToSearch, string stringToFind)
{
  var maxIndex = stringToSearch.Length - stringToFind.Length;
  // String, which we want to find is bigger than string, which we want to search
  if (maxIndex < 0) return false;

  for (int i = 0; i <= maxIndex; i++)
  {
    int j;
    for (j = 0; j < stringToFind.Length; j++)
      // If we have different letters, stop comparing and go to next iteration of outer loop
      if (stringToSearch[i + j] != stringToFind[j]) break;
    // If we reached last iteration of a loop then we found the string
    if (j == stringToFind.Length) return true;
  }

  // If we reached this point, we didn't find the string
  return false;
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Thank you Michal, that what i was looking for! Can you explain me the outter loop? I dont understand why you are using the maxIndex for it. – flubix Jan 10 '19 at 13:07
  • 1
    Consider simple example: you have two strings `"flubix"` and `"bix"`. I use `maxIndex` to determine when to stop checking, i.e. `maxIndex` would be here 3, so we check up to third letter, because if we'd go further, we won't get a match anyway, because we would try to fit `bix` in `ix` (substring of `fubix`). – Michał Turczyn Jan 10 '19 at 13:10
  • Ah makes sense. I should have think about this a bit longer, thank you! – flubix Jan 10 '19 at 13:18