Skip to content

Домашнее задание 3.2. Поиск подстроки. Разгуляева А.И. #15

Open
ada1ra wants to merge 1 commit intomainfrom
hw_3-2_subString
Open

Домашнее задание 3.2. Поиск подстроки. Разгуляева А.И. #15
ada1ra wants to merge 1 commit intomainfrom
hw_3-2_subString

Conversation

@ada1ra
Copy link
Owner

@ada1ra ada1ra commented Dec 11, 2025

No description provided.

@ada1ra ada1ra requested a review from chernishev December 11, 2025 09:14
int count = 0;

char* mainStringEnd = mainString + strlen(mainString); // вычисляем указатель на конец строки
while (temp != NULL && temp < mainStringEnd) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Указатели лучше не сравнивать с помощью <. По крайней мере, пока в этом не возникает особой нужды. Здесь должно быть temp != mainStringEnd.

int count = 0;

char* mainStringEnd = mainString + strlen(mainString); // вычисляем указатель на конец строки
while (temp != NULL && temp < mainStringEnd) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, здесь temp может быть NULL только в одном случае: если изначально передали mainString = NULL. А это надо проверить отдельно до вызова strlen, иначе может сломаться.

Вы ведь сразу присвоили ему mainString (допустим, что он NULL не бывает), а потом присваиваете только result, который каждый раз проверяется на NULL.

char* mainStringEnd = mainString + strlen(mainString); // вычисляем указатель на конец строки
while (temp != NULL && temp < mainStringEnd) {
char* result = strstr(temp, subString); // поиск указателя на первый элемент найденной подстроки
if (result && temp) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот здесь уже точно temp не может стать NULL. Независимо от того, проверите Вы это в условии цикла, или нет.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants