Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- int strStr(const char *text, const char *pattern);
- int strLen(const char *str);
- bool strCheck(const char *p, const char *pattern);
- int main()
- {
- setlocale(0, "Russian");
- std::cout << "Programm started\n";
- struct test {
- int ret_value;
- const char *text;
- const char *pattern;
- };
- test tests[] = {
- {0, "", ""}, //0
- {0, "a", ""}, //1
- {0, "a", "a"}, //2
- {-1, "a", "b"}, //3
- {0, "aa", ""}, //4
- {0, "aa", "a"}, //5
- {0, "ab", "a"}, //6
- {1, "ba", "a"}, //7
- {-1, "bb", "a"}, //8
- {0, "aaa", ""}, //9
- {0, "aaa", "a"}, //10
- {1, "abc", "b"}, //11
- {2, "abc", "c"}, //12
- {-1, "abc", "d"}, //13
- {-1, "a", "aa"}, //14
- {-1, "a", "ba"}, //15
- {-1, "a", "ab"}, //16
- {-1, "a", "bb"}, //17
- {-1, "a", "aaa"}, //18
- {-1, "aa", "aaa"}, //19
- {0, "aaa", "aaa"}, //20
- {0, "aaab", "aaa"}, //21
- {1, "baaa", "aaa"}, //22
- {1, "baaaa", "aaa"}, //23
- {1, "baaab", "aaa"}, //24
- {-1, "abd", "abc"}, //25
- {2, "ababc", "abc"}, //26
- {3, "abdabc", "abc"}, //27
- {-1, "", "a"}, //28
- {2, "asasaf", "asaf"}, //29
- {2, "ababac", "abac"} //30
- };
- for (int i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
- int ret = strStr(tests[i].text, tests[i].pattern);
- (tests[i].ret_value == ret) ? cout << "OK" : cout << "Failed";
- cout << " : " << i << " (" << tests[i].ret_value << " : " << ret << ")" << endl;
- }
- system("pause");
- return 0;
- }
- //находит первое вхождение в строке text некоторого шаблона pattern
- int strStr(const char *text, const char *pattern)
- {
- if (*pattern == '\0')
- return 0;
- else if (*text == '\0')
- return -1;
- int N = strLen(text);
- for (int i = 0; i < N; i++)
- {
- if (strCheck(text + i, pattern))
- return i;
- }
- return -1;
- }
- //находит длину строки, не учитывая нулевой символ
- int strLen(const char *str)
- {
- int i = 0;
- while ( *(str + i) != '\0' )
- i++;
- return i;
- }
- //проверяет соответствие некоторой части строки шаблону
- bool strCheck(const char *p, const char *pattern)
- {
- for (; *pattern != '\0'; pattern++)
- {
- if (*p != *pattern)
- return false;
- p++;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement