Complete the code to get the length of the pattern string.
int patternLength = pattern.[1];Length() as if it were a method.Count which is not a string property.The Length property returns the number of characters in the string.
Complete the code to compare characters of text and pattern at given indices.
if (text[i + j] == pattern.[1]) {
Substring which returns a string, not a character.ElementAt without importing LINQ.Use the indexer [j] to get the character at position j in the pattern string.
Fix the error in the loop condition to avoid going out of bounds in the text string.
for (int i = 0; i <= text.Length - [1]; i++) {
pattern.Length + 1 which causes index out of range.pattern.Length - 1 which misses some valid positions.We subtract the pattern length to ensure the pattern fits inside the text when matching.
Fill both blanks to complete the inner loop that checks each character of the pattern against the text.
for (int j = 0; j < [1]; j++) { if (text[i + j] != pattern.[2]) { break; } }
text.Length for the loop limit which is incorrect.[i] instead of [j].The inner loop runs for the length of the pattern, and compares characters at index j in the pattern.
Fill all three blanks to complete the naive pattern matching function that returns the first index of the pattern in the text or -1 if not found.
public static int NaiveSearch(string text, string pattern) {
int n = text.Length;
int m = pattern.[1];
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (text[i + j] != pattern.[2]) {
break;
}
}
if (j == [3]) {
return i;
}
}
return -1;
}[i] instead of [j] to access pattern characters.j to n instead of m.Length() as a method instead of property.The pattern length is stored in m. Characters are compared at index j in the pattern. If all characters match (j == m), return the current index i.
