From 0703fece88e507871b94db6de30531b769a6121a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gu=C3=A9len?= Date: Sun, 2 Mar 2025 18:07:46 +0100 Subject: [PATCH] Expansion: Added a brute force word search Just in case --- src/word_search/word_search.c | 25 +++++++++++++++++++++++-- src/word_search/word_search.h | 8 +++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/word_search/word_search.c b/src/word_search/word_search.c index 9fc2731..8cad5c3 100644 --- a/src/word_search/word_search.c +++ b/src/word_search/word_search.c @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/02 14:27:58 by jguelen #+# #+# */ -/* Updated: 2025/03/02 17:51:04 by jguelen ### ########.fr */ +/* Updated: 2025/03/02 18:06:54 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ static int *create_kmp_array(char *needle, size_t needle_len) } /* -** @Param Should only be provided with non NULL or empty arguments +** @Param Should only be provided with arguments that are neither NULL or empty ** Could be extended to report all occurrences of needle in haystack ** but for now only reports the first. ** (cf http://monge.univ-mlv.fr/~lecroq/string/node8.html#SECTION0080) @@ -80,3 +80,24 @@ ssize_t word_search_kmp(char *haystack, char *needle) free(kmp); return (-1); } + +/* +** Should not be given NULL or empty arguments. +*/ +ssize_t word_search_brute(char *haystack, char *needle) +{ + ssize_t i; + size_t needle_len; + size_t haystack_len; + + needle_len = ft_strlen(needle); + haystack_len = ft_strlen(haystack); + i = 0; + while (i <= (haystack_len - needle_len)) + { + if (ft_memcmp(haystack + i, needle, needle_len) == 0) + return (i); + i++; + } + return (-1); +} diff --git a/src/word_search/word_search.h b/src/word_search/word_search.h index a76291a..78da314 100644 --- a/src/word_search/word_search.h +++ b/src/word_search/word_search.h @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/02 14:10:36 by jguelen #+# #+# */ -/* Updated: 2025/03/02 14:30:16 by jguelen ### ########.fr */ +/* Updated: 2025/03/02 17:56:02 by jguelen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,11 @@ ** -1 if no such occurrence was found and -2 in case of allocation error. */ ssize_t word_search_kmp(char *haystack, char *needle); +/* +** The simple, familiar brute force searching algorithm of a word in a text. +** Returns the index where an occurrence of needle is found in haystack, or +** -1 if no such occurrence exists. +*/ +ssize_t word_search_brute(char *haystack, char *needle); #endif