1
0
forked from 131/lab3_test
Files
lab3_test/str_lib.c
Ваше Имя cc08212e54 sdds
2025-11-01 06:15:23 -04:00

51 lines
1.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <string.h>
/*
* Вернуть длину строки.
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
*/
int
mystrlen(const char *s)
{
// <YOURCODE>
int len = 0;
while (s[len] != '\0') {
len++;
}
return len;
}
/*
* Найти индекс, с которого строка s2 присутствует в строке s1
* или -1
*/
int
mystr_idx(const char *s1, const char *s2)
{
// <YOURCODE>
int i = 0; // Индекс для s1
// Если s2 пустая строка, возвращаем 0 (поведение как у strstr)
if (s2[0] == '\0') {
return 0;
}
while (s1[i] != '\0') {
int j = 0; // Индекс для s2
// Внутренний цикл: сравниваем символы
while (s1[i + j] != '\0' && s2[j] != '\0' && s1[i + j] == s2[j]) {
j++;
}
// Если j дошел до конца s2, значит s2 найдена
if (s2[j] == '\0') {
return i; // Возвращаем индекс начала вхождения
}
i++; // Сдвигаем i, пробуем найти со следующего символа s1
}
return -1;
}