57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
#include <stdio.h>
|
|
|
|
int mystr_idx(char *str, char *substr) {
|
|
if (str == NULL || substr == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
if (*substr == '\0'){
|
|
return 0;
|
|
}
|
|
|
|
int i=0;
|
|
while (str[i] != '\0') {
|
|
int j = 0;
|
|
int temp_i = i;
|
|
|
|
while (substr[j] != '\0' && str[temp_i] != '\0' && str[temp_i] == substr[j]) {
|
|
temp_i++;
|
|
j++;
|
|
}
|
|
|
|
if (substr[j] == '\0') {
|
|
return i;
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void test_mystr_idx() {
|
|
printf("mystr_idx(\"helloworld\", \"world\") = %d (ожидается: 5)\n",
|
|
mystr_idx("helloworld", "world"));
|
|
printf("mystr_idx(\"helloworld\", \"helloworld\") = %d (ожидается: 0)\n",
|
|
mystr_idx("helloworld", "helloworld"));
|
|
printf("mystr_idx(\"helloworld\", \"foo\") = %d (ожидается: -1)\n",
|
|
mystr_idx("helloworld", "foo"));
|
|
printf("mystr_idx(\"helloworld\", \"hello\") = %d (ожидается: 0)\n",
|
|
mystr_idx("helloworld", "hello"));
|
|
printf("mystr_idx(\"helloworld\", \"low\") = %d (ожидается: 3)\n",
|
|
mystr_idx("helloworld", "low"));
|
|
printf("mystr_idx(\"abcabc\", \"abc\") = %d (ожидается: 0)\n",
|
|
mystr_idx("abcabc", "abc"));
|
|
printf("mystr_idx(\"\", \"test\") = %d (ожидается: -1)\n",
|
|
mystr_idx("", "test"));
|
|
printf("mystr_idx(\"test\", \"\") = %d (ожидается: 0)\n",
|
|
mystr_idx("test", ""));
|
|
}
|
|
|
|
int main() {
|
|
test_mystr_idx();
|
|
return 0;
|
|
}
|