how can i return the first position of the first occurrence of a substring in a strin
Code:
public static int StringMatch(string baseStr, string compStr) {
//bool b = false;
int count = 0;
//int index = 0;
for (int i = 0; i < baseStr.Length; i++)
{
for (int j = 0; j < compStr.Length; j++)
{
if (baseStr[i] == compStr[j])
{
count++;
}
if (count == compStr.Length)
{
return i - compStr.Length + 1;
}
}
}
return -1;
}
static void Main(string[] args)
{
string txt = "dhgrogood";
string phrase = "good";
int res = StringMatch(txt, phrase);
Console.WriteLine(res);
}
}
}