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.

3
  • 2
    Cheat code ;) Commented Jan 10, 2019 at 12:39
  • 1
    If you aren't allowed to cheat, then here is something similar, notice nested loop, you'll need it too. Commented Jan 10, 2019 at 12:42
  • im not allowed to cheat or to use any other implemented methods, but the second links works for me. thanks! Commented Jan 10, 2019 at 13:06

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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).
Ah makes sense. I should have think about this a bit longer, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.