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.