Онлайн компилятор C

#include <stdio.h> int main() { char text[1001]; char pattern[101]; fgets(text, sizeof(text), stdin); fgets(pattern, sizeof(pattern), stdin); int count = 0; int i = 0; int pat_len = 0; while (pattern[pat_len] != '\0' && pattern[pat_len] != '\n') { pat_len++; } while (text[i] != '\0' && text[i] != '\n') { int j = 0; while (j < pat_len && text[i + j] == pattern[j]) { j++; } if (j == pat_len) { count++; } i++; } printf("%d", count); return 0; }
This code reads two lines of input: a text string and a pattern string. It then counts how many times the pattern appears as a contiguous substring within the text. The counting uses a simple character-by-character comparison starting at each position in the text.

- The variable `pat_len` is computed by counting characters in `pattern` until a newline or null terminator. This length is used to compare the pattern against substrings of `text`. However, note that `fgets` includes the newline character in the buffer if there is space, so the loop that calculates `pat_len` correctly stops at the newline. But the same newline handling is not applied to `text` in the inner comparison loop — the inner loop compares `text[i + j]` with `pattern[j]`, but `pattern[j]` may be a newline if `pat_len` was not set correctly, or the comparison may go beyond the actual text length.

- The outer loop increments `i` by 1 each time, even when a match is found. This means overlapping matches are counted. For example, in "aaa" with pattern "aa", it counts two matches (positions 0 and 1). Check if this is the intended behavior for the task.