Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <conio.h>
- using namespace std;
- int length(const char *str)
- {
- const char *temp = str;
- while (*temp) temp++;
- return (temp - str);
- }
- int funcstr(const char *text, const char *pattern)
- {
- unsigned count = 0;
- unsigned lengthTxt = 0;
- unsigned lengthPttrn = 0;
- lengthTxt = length(text);
- lengthPttrn = length(pattern);
- if (lengthPttrn > lengthTxt) return -1;
- if ((*pattern == '\0') || (*text == '\0') && (*pattern == '\0')) return 0;
- const char *tempTxt = text;
- const char *tempPttrn = pattern;
- while (*tempTxt!= '\0')
- {
- if (*tempTxt != *tempPttrn)
- {
- count = 0;
- tempPttrn = pattern;
- }
- if (*tempTxt == *tempPttrn)
- {
- count++;
- tempPttrn++;
- }
- if (count == lengthPttrn) return (tempTxt - text ) - count + 1;
- tempTxt++;
- }
- return -1;
- }
- void test()
- {
- (0 == funcstr("", "")) ? cout << "OK : 1" << " (" << 0 << " : " << (0 == funcstr("", "")) << " )" << endl : cout << "Failed : 1" << " (" << 0 << " : " << (0 == funcstr("", "")) << " )" << endl;
- (0 == funcstr("a", "")) ? cout << "OK : 2" << " (" << 0 << " : " << (0 == funcstr("a", "")) << " )" << endl : cout << "Failed : 2" << " (" << 0 << " : " << (0 == funcstr("a", "")) << " )" << endl;
- (0 == funcstr("a", "a")) ? cout << "OK : 3" << " (" << 0 << " : " << (0 == funcstr("a", "a")) << " )" << endl : cout << "Failed : 3" << " (" << 0 << " : " << (0 == funcstr("a", "a")) << " )" << endl;
- (-1 == funcstr("a", "b")) ? cout << "OK : 4" << " (" << -1 << " : " << (-1 == funcstr("a", "b")) << " )" << endl : cout << "Failed : 4" << " (" << -1 << " : " << (-1 == funcstr("a", "b")) << " )" << endl;
- (0 == funcstr("aa", "")) ? cout << "OK : 5" << " (" << 0 << " : " << (0 == funcstr("aa", "")) << " )" << endl : cout << "Failed : 5" << " (" << 0 << " : " << (0 == funcstr("aa", "")) << " )" << endl;
- (0 == funcstr("aa", "a")) ? cout << "OK : 6" << " (" << 0 << " : " << (0 == funcstr("aa", "a")) << " )" << endl : cout << "Failed : 6" << " (" << 0 << " : " << (0 == funcstr("aa", "a")) << " )" << endl;
- (0 == funcstr("ab", "a")) ? cout << "OK : 7" << " (" << 0 << " : " << (0 == funcstr("ab", "a")) << " )" << endl : cout << "Failed : 7" << " (" << 0 << " : " << (0 == funcstr("ab", "a")) << " )" << endl;
- (1 == funcstr("ba", "a")) ? cout << "OK : 8" << " (" << 1 << " : " << (1 == funcstr("ba", "a")) << " )" << endl : cout << "Failed : 8" << " (" << 1 << " : " << (1 == funcstr("ba", "a")) << " )" << endl;
- (-1 == funcstr("bb", "a")) ? cout << "OK : 9" << " (" << -1 << " : " << (-1 == funcstr("bb", "a")) << " )" << endl : cout << "Failed : 9" << " (" << -1 << " : " << (-1 == funcstr("bb", "a")) << " )" << endl;
- (0 == funcstr("aaa", "")) ? cout << "OK : 10" << " (" << 0 << " : " << (0 == funcstr("aaa", "")) << " )" << endl : cout << "Failed : 10" << " (" << 0 << " : " << (0 == funcstr("aaa", "")) << " )" << endl;
- (0 == funcstr("aaa", "a")) ? cout << "OK : 11" << " (" << 0 << " : " << (0 == funcstr("aaa", "a")) << " )" << endl : cout << "Failed : 11" << " (" << 0 << " : " << (0 == funcstr("aaa", "a")) << " )" << endl;
- (1 == funcstr("abc", "b")) ? cout << "OK : 12" << " (" << 1 << " : " << (1 == funcstr("abc", "b")) << " )" << endl : cout << "Failed : 12" << " (" << 1 << " : " << (1 == funcstr("abc", "b")) << " )" << endl;
- (2 == funcstr("abc", "c")) ? cout << "OK : 13" << " (" << 2 << " : " << (2 == funcstr("abc", "c")) << " )" << endl : cout << "Failed : 13" << " (" << 2 << " : " << (2 == funcstr("abc", "c")) << " )" << endl;
- (-1 == funcstr("abc", "d")) ? cout << "OK : 14" << " (" << -1 << " : " << (-1 == funcstr("abc", "d")) << " )" << endl : cout << "Failed : 14" << " (" << -1 << " : " << (-1 == funcstr("abc", "d")) << " )" << endl;
- (-1 == funcstr("a", "aa")) ? cout << "OK : 15" << " (" << -1 << " : " << (-1 == funcstr("a", "aa")) << " )" << endl : cout << "Failed : 15" << " (" << -1 << " : " << (-1 == funcstr("a", "aa")) << " )" << endl;
- (-1 == funcstr("a", "ba")) ? cout << "OK : 16" << " (" << -1 << " : " << (-1 == funcstr("a", "ba")) << " )" << endl : cout << "Failed : 16" << " (" << -1 << " : " << (-1 == funcstr("a", "ba")) << " )" << endl;
- (-1 == funcstr("a", "ab")) ? cout << "OK : 17" << " (" << -1 << " : " << (-1 == funcstr("a", "ab")) << " )" << endl : cout << "Failed : 17" << " (" << -1 << " : " << (-1 == funcstr("a", "ab")) << " )" << endl;
- (-1 == funcstr("a", "bb")) ? cout << "OK : 18" << " (" << -1 << " : " << (-1 == funcstr("a", "bb")) << " )" << endl : cout << "Failed : 18" << " (" << -1 << " : " << (-1 == funcstr("a", "bb")) << " )" << endl;
- (-1 == funcstr("a", "aaa")) ? cout << "OK : 19" << " (" << -1 << " : " << (-1 == funcstr("a", "aaa")) << " )" << endl : cout << "Failed : 19" << " (" << -1 << " : " << (-1 == funcstr("a", "aaa")) << " )" << endl;
- (-1 == funcstr("aa", "aaa")) ? cout << "OK : 20" << " (" << -1 << " : " << (-1 == funcstr("aa", "aaa")) << " )" << endl : cout << "Failed : 20" << " (" << -1 << " : " << (-1 == funcstr("aa", "aaa")) << " )" << endl;
- (0 == funcstr("aaa", "aaa")) ? cout << "OK : 21" << " (" << 0 << " : " << (0 == funcstr("aaa", "aaa")) << " )" << endl : cout << "Failed : 21" << " (" << 0 << " : " << (0 == funcstr("aaa", "aaa")) << " )" << endl;
- (0 == funcstr("aaab", "aaa")) ? cout << "OK : 22" << " (" << 0 << " : " << (0 == funcstr("aaab", "aaa")) << " )" << endl : cout << "Failed : 22" << " (" << 0 << " : " << (0 == funcstr("aaab", "aaa")) << " )" << endl;
- (1 == funcstr("baaa", "aaa")) ? cout << "OK : 23" << " (" << 1 << " : " << (1 == funcstr("baaa", "aaa")) << " )" << endl : cout << "Failed : 23" << " (" << 1 << " : " << (1 == funcstr("baaa", "aaa")) << " )" << endl;
- (1 == funcstr("baaaa", "aaa")) ? cout << "OK : 24" << " (" << 1 << " : " << (1 == funcstr("baaaa", "aaa")) << " )" << endl : cout << "Failed : 24" << " (" << 1 << " : " << (1 == funcstr("baaaa", "aaa")) << " )" << endl;
- (1 == funcstr("baaab", "aaa")) ? cout << "OK : 25" << " (" << 1 << " : " << (1 == funcstr("baaab", "aaa")) << " )" << endl : cout << "Failed : 25" << " (" << 1 << " : " << (1 == funcstr("baaab", "aaa")) << " )" << endl;
- (-1 == funcstr("abd", "abc")) ? cout << "OK : 26" << " (" << -1 << " : " << (-1 == funcstr("abd", "abc")) << " )" << endl : cout << "Failed : 26" << " (" << -1 << " : " << (-1 == funcstr("abd", "abc")) << " )" << endl;
- (2 == funcstr("ababc", "abc")) ? cout << "OK : 27" << " (" << 2 << " : " << (2 == funcstr("ababc", "abc")) << " )" << endl : cout << "Failed : 27" << " (" << 2 << " : " << (2 == funcstr("ababc", "abc")) << " )" << endl;
- (3 == funcstr("abdabc", "abc")) ? cout << "OK : 28" << " (" << 3 << " : " << (3 == funcstr("abdabc", "abc")) << " )" << endl : cout << "Failed : 28" << " (" << 3 << " : " << (3 == funcstr("abdabc", "abc")) << " )" << endl;
- }
- int main()
- {
- std::string stroka;
- std::string pattern;
- std::cout << "Vvedite stroku :";
- std::getline(std::cin, stroka);
- std::cout << "Vvedite shablon:";
- std::getline(std::cin, pattern);
- int res = funcstr(stroka.c_str(), pattern.c_str());
- //test();
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement