Bird
0
0
DSA Cprogramming~10 mins

Substring Search Patterns in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the first occurrence of a substring in a string using strstr.

DSA C
char *result = strstr(text, [1]);
Drag options to blanks, or click blank then click option'
Apattern
Btext
CNULL
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole text as the substring to search for.
Using NULL instead of the substring.
Using the result pointer as the substring.
2fill in blank
medium

Complete the code to compare two characters in the text and pattern during substring search.

DSA C
if (text[i] == [1][j]) {
Drag options to blanks, or click blank then click option'
Asubstring
Btext
Cresult
Dpattern
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing text[i] with text[j] instead of pattern[j].
Using result or substring variables incorrectly.
3fill in blank
hard

Fix the error in the loop condition to avoid reading beyond the text length during substring search.

DSA C
for (int i = 0; i <= text_length - [1]; i++) {
Drag options to blanks, or click blank then click option'
Atext_length
Bpattern_length
Cresult_length
Dsubstring_length
Attempts:
3 left
💡 Hint
Common Mistakes
Using text_length instead of pattern_length in the subtraction.
Using result_length or substring_length which are undefined.
4fill in blank
hard

Fill both blanks to correctly check if the substring matches the text segment.

DSA C
if (j == [1]) {
    return [2];
}
Drag options to blanks, or click blank then click option'
Apattern_length
Bi
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or 1 instead of the starting index.
Checking j against text_length instead of pattern_length.
5fill in blank
hard

Fill all three blanks to complete the naive substring search function.

DSA C
int naive_search(char *text, char *pattern) {
    int text_len = strlen(text);
    int pattern_len = strlen(pattern);
    for (int i = 0; i <= text_len - [1]; i++) {
        int j = 0;
        while (j < [2] && text[i + j] == pattern[[3]]) {
            j++;
        }
        if (j == pattern_len) {
            return i;
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Apattern_len
Cj
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j to index pattern characters.
Using text_len instead of pattern_len in loop conditions.