forked from 131/lab3_test
Допилил файлик mystrlen, mystr_idx
This commit is contained in:
30
str_lib.c
30
str_lib.c
@@ -7,7 +7,11 @@
|
||||
int
|
||||
mystrlen(const char *s)
|
||||
{
|
||||
// <YOURCODE>
|
||||
int len = 0;
|
||||
while (s[len] != '\0') {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -17,7 +21,29 @@ mystrlen(const char *s)
|
||||
int
|
||||
mystr_idx(const char *s1, const char *s2)
|
||||
{
|
||||
// <YOURCODE>
|
||||
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;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user