1
0
forked from TEX/lab3_test
Files
lab3_test/str_lib.c
etrushko05 f7fdfe75bc 18
2025-11-15 04:53:50 -05:00

45 lines
942 B
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)
{
int length = 0;
while (s[length] != '\0') {
length++;
}
return length;
}
/*
* Найти индекс, с которого строка s2 присутствует в строке s1
* или -1
*/
int
mystr_idx(const char *s1, const char *s2)
{
int str_len = mystrlen(s1);
int substr_len = mystrlen(s2);
if (substr_len > str_len) {
return -1;
}
for (int i = 0; i <= str_len - substr_len; i++) {
int j;
for (j = 0; j < substr_len; j++) {
if (s1[i + j] != s2[j]) {
break;
}
}
if (j == substr_len) {
return i;
}
}
return -1;
}