Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the outer loop from the first character of the text.
DSA C
for (int i = 0; i <= n - m; i[1]) { // pattern matching logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator -- causes the loop to run incorrectly.
Using '+=' without a value is invalid syntax.
✗ Incorrect
The outer loop should increment i by 1 each time to check every possible starting position in the text.
2fill in blank
mediumComplete the code to compare characters of the pattern and text inside the inner loop.
DSA C
for (int j = 0; j < m; j++) { if (text[i + j] [1] pattern[j]) { break; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes the loop to break on matching characters, which is wrong.
Using '>' or '<' compares ASCII values but is not suitable here.
✗ Incorrect
We break the loop when characters do not match, so the condition should check for inequality.
3fill in blank
hardFix the error in the condition that checks if the pattern was found completely.
DSA C
if (j [1] m) { printf("Pattern found at index %d\n", i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' causes false positives before full match.
Using '>' is incorrect because j cannot be greater than m here.
✗ Incorrect
If j equals m, it means all characters matched and the pattern is found.
4fill in blank
hardFill both blanks to correctly declare and initialize the lengths of text and pattern.
DSA C
int n = strlen([1]); int m = strlen([2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like 'string' or 'input'.
Mixing up text and pattern variables.
✗ Incorrect
We use strlen on the text and pattern strings to get their lengths.
5fill in blank
hardFill all three blanks to complete the naive pattern matching function header and variable declarations.
DSA C
void [1](char *[2], char *[3]) { int n = strlen(text); int m = strlen(pattern); // matching logic }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect function names like 'search'.
Swapping parameter names or using undefined names.
✗ Incorrect
The function is named naiveSearch and takes text and pattern as parameters.
