From 8cc668bdd07e266b8b93e550717388ad65f0a08a Mon Sep 17 00:00:00 2001 From: Arseny Date: Sat, 15 Nov 2025 04:27:54 -0500 Subject: [PATCH] Pervaya chast zadania --- str_lib.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/str_lib.c b/str_lib.c index fa1d7db..acca037 100644 --- a/str_lib.c +++ b/str_lib.c @@ -1,23 +1,39 @@ #include - -/* - * Вернуть длину строки. - * Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0') - */ int mystrlen(const char *s) { - // + // + int len = 0; + while (s[len] != '\0') { + len++; + } + return len; } -/* - * Найти индекс, с которого строка s2 присутствует в строке s1 - * или -1 - */ int mystr_idx(const char *s1, const char *s2) { - // - return -1; + // + int len1 = mystrlen(s1); + int len2 = mystrlen(s2); + + if (len2 == 0) { + return 0; // Пустая строка всегда в начале + } + + for (int i = 0; i <= len1 - len2; i++) { + int j; + for (j = 0; j < len2; j++) { + if (s1[i + j] != s2[j]) { + break; // Не совпадает + } + } + if (j == len2) { + return i; // Найдено полное совпадение + } + } + + return -1; } +