Files
lab3_test/str_lib.c

50 lines
814 B
C
Raw Permalink Normal View History

2023-03-25 03:58:02 +03:00
#include <string.h>
/*
* Вернуть длину строки.
* Строки в C -- это массив символов, в конце которого находится нулевой символ ( '\0')
*/
int
mystrlen(const char *s)
{
int len = 0;
while (s[len] != '\0') {
len++;
}
return len;
2023-03-25 03:58:02 +03:00
}
/*
* Найти индекс, с которого строка s2 присутствует в строке s1
* или -1
*/
int
mystr_idx(const char *s1, const char *s2)
{
if (s1 == NULL || s2 == NULL)
return -1;
if (s2[0] == '\0')
return 0;
int len1 = mystrlen(s1);
int len2 = mystrlen(s2);
if (len2 > len1)
return -1;
for (int i = 0; i <=len1 - len2; i++) {
int j = 0;
while (j < len2 && s1[i + j] == s2[j]) {
j++;
}
if (j == len2)
return i;
}
2023-03-25 03:58:02 +03:00
return -1;
}