strstr() function | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

NICE! Just what I was looking for. I read books on C but, I can't remember everything. Short video's like yours are a big help!

michaelpatterson
Автор

Hello coatch 😉, Thanks for the great video as usual!!! yeah, even thought the function does not consider the null terminator of the string to be found, it does need it to know when to stop the comparison... the magic point is when you sais this sentence : "the first complete match " +" in a row"!!! because when you understand how the function finds out the substring, it's abvious to get the whole function!!!

zoquevil
Автор

I couldn't find the follow up video on how to implement it, could you assist me with that?

HYPRMAN_ZERO
Автор

char* str_str(const char* str, const char* sub_str) {
if (*sub_str == '\0') { // If the substring is empty, return the whole string
return (char*)str;
}

size_t str_len = strlen(str);
size_t sub_str_len = strlen(sub_str);

for (int i = 0; i < str_len; i++) {
if (str[i] == sub_str[0] && (i + sub_str_len <= str_len)) {
for (int j = 1; j < sub_str_len; j++) {
if (str[i + j] != sub_str[j]) {
// Mismatch found, break out of the inner loop
} else {
(j == sub_str_len - 1) {
str + i; // Full substring found, return a pointer to its starting position

}
}
}
}

return NULL; // Substring not found, return NULL
}

deepblackoutlaw
visit shbcf.ru