Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78f8782c9a |
39
str_lib.c
39
str_lib.c
@@ -8,6 +8,15 @@ int
|
||||
mystrlen(const char *s)
|
||||
{
|
||||
// <YOURCODE>
|
||||
if (s == NULL) {
|
||||
return -1; // или 0, в зависимости от требований
|
||||
}
|
||||
|
||||
int length = 0;
|
||||
while (s[length] != '\0') {
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -18,6 +27,36 @@ int
|
||||
mystr_idx(const char *s1, const char *s2)
|
||||
{
|
||||
// <YOURCODE>
|
||||
if (s1 == NULL || s2 == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int len1 = mystrlen(s1);
|
||||
int len2 = mystrlen(s2);
|
||||
|
||||
// Если подстрока пустая, возвращаем 0
|
||||
if (len2 == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Если подстрока длиннее строки, она не может содержаться в ней
|
||||
if (len2 > len1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Поиск подстроки
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user