Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. int length(const char *str)
  7. {
  8.     const char *temp = str;
  9.     while (*temp) temp++;
  10.     return (temp - str);
  11. }
  12.  
  13.  
  14. int funcstr(const char *text, const char *pattern)
  15. {
  16.     unsigned count = 0;
  17.     unsigned lengthTxt = 0;
  18.     unsigned lengthPttrn = 0;
  19.     lengthTxt = length(text);
  20.     lengthPttrn = length(pattern);
  21.  
  22.     if (lengthPttrn > lengthTxt) return -1;
  23.  
  24.     if ((*pattern == '\0') || (*text == '\0') && (*pattern == '\0')) return 0;
  25.     const char *tempTxt = text;
  26.     const char *tempPttrn = pattern;
  27.  
  28.     while (*tempTxt!= '\0')
  29.     {  
  30.        
  31.          if (*tempTxt != *tempPttrn)
  32.  
  33.         {
  34.             count = 0;
  35.             tempPttrn = pattern;
  36.         }
  37.        
  38.          if (*tempTxt == *tempPttrn)
  39.    
  40.         {
  41.             count++;
  42.             tempPttrn++;
  43.         }
  44.  
  45.        
  46.  
  47.         if (count == lengthPttrn) return (tempTxt - text ) - count + 1;
  48.        
  49.         tempTxt++;
  50.  
  51.  
  52.     }
  53.  
  54.     return -1;
  55. }
  56.  
  57. void test()
  58. {
  59.     (0 == funcstr("", "")) ? cout << "OK : 1" << " (" << 0 << " : " << (0 == funcstr("", "")) << " )" << endl : cout << "Failed : 1" << " (" << 0 << " : " << (0 == funcstr("", "")) << " )" << endl;
  60.     (0 == funcstr("a", "")) ? cout << "OK : 2" << " (" << 0 << " : " << (0 == funcstr("a", "")) << " )" << endl : cout << "Failed : 2" << " (" << 0 << " : " << (0 == funcstr("a", "")) << " )" << endl;
  61.     (0 == funcstr("a", "a")) ? cout << "OK : 3" << " (" << 0 << " : " << (0 == funcstr("a", "a")) << " )" << endl : cout << "Failed : 3" << " (" << 0 << " : " << (0 == funcstr("a", "a")) << " )" << endl;
  62.     (-1 == funcstr("a", "b")) ? cout << "OK : 4" << " (" << -1 << " : " << (-1 == funcstr("a", "b")) << " )" << endl : cout << "Failed : 4" << " (" << -1 << " : " << (-1 == funcstr("a", "b")) << " )" << endl;
  63.  
  64.     (0 == funcstr("aa", "")) ? cout << "OK : 5" << " (" << 0 << " : " << (0 == funcstr("aa", "")) << " )" << endl : cout << "Failed : 5" << " (" << 0 << " : " << (0 == funcstr("aa", "")) << " )" << endl;
  65.     (0 == funcstr("aa", "a")) ? cout << "OK : 6" << " (" << 0 << " : " << (0 == funcstr("aa", "a")) << " )" << endl : cout << "Failed : 6" << " (" << 0 << " : " << (0 == funcstr("aa", "a")) << " )" << endl;
  66.     (0 == funcstr("ab", "a")) ? cout << "OK : 7" << " (" << 0 << " : " << (0 == funcstr("ab", "a")) << " )" << endl : cout << "Failed : 7" << " (" << 0 << " : " << (0 == funcstr("ab", "a")) << " )" << endl;
  67.     (1 == funcstr("ba", "a")) ? cout << "OK : 8" << " (" << 1 << " : " << (1 == funcstr("ba", "a")) << " )" << endl : cout << "Failed : 8" << " (" << 1 << " : " << (1 == funcstr("ba", "a")) << " )" << endl;
  68.     (-1 == funcstr("bb", "a")) ? cout << "OK : 9" << " (" << -1 << " : " << (-1 == funcstr("bb", "a")) << " )" << endl : cout << "Failed : 9" << " (" << -1 << " : " << (-1 == funcstr("bb", "a")) << " )" << endl;
  69.  
  70.     (0 == funcstr("aaa", "")) ? cout << "OK : 10" << " (" << 0 << " : " << (0 == funcstr("aaa", "")) << " )" << endl : cout << "Failed : 10" << " (" << 0 << " : " << (0 == funcstr("aaa", "")) << " )" << endl;
  71.     (0 == funcstr("aaa", "a")) ? cout << "OK : 11" << " (" << 0 << " : " << (0 == funcstr("aaa", "a")) << " )" << endl : cout << "Failed : 11" << " (" << 0 << " : " << (0 == funcstr("aaa", "a")) << " )" << endl;
  72.     (1 == funcstr("abc", "b")) ? cout << "OK : 12" << " (" << 1 << " : " << (1 == funcstr("abc", "b")) << " )" << endl : cout << "Failed : 12" << " (" << 1 << " : " << (1 == funcstr("abc", "b")) << " )" << endl;
  73.     (2 == funcstr("abc", "c")) ? cout << "OK : 13" << " (" << 2 << " : " << (2 == funcstr("abc", "c")) << " )" << endl : cout << "Failed : 13" << " (" << 2 << " : " << (2 == funcstr("abc", "c")) << " )" << endl;
  74.     (-1 == funcstr("abc", "d")) ? cout << "OK : 14" << " (" << -1 << " : " << (-1 == funcstr("abc", "d")) << " )" << endl : cout << "Failed : 14" << " (" << -1 << " : " << (-1 == funcstr("abc", "d")) << " )" << endl;
  75.  
  76.     (-1 == funcstr("a", "aa")) ? cout << "OK : 15" << " (" << -1 << " : " << (-1 == funcstr("a", "aa")) << " )" << endl : cout << "Failed : 15" << " (" << -1 << " : " << (-1 == funcstr("a", "aa")) << " )" << endl;
  77.     (-1 == funcstr("a", "ba")) ? cout << "OK : 16" << " (" << -1 << " : " << (-1 == funcstr("a", "ba")) << " )" << endl : cout << "Failed : 16" << " (" << -1 << " : " << (-1 == funcstr("a", "ba")) << " )" << endl;
  78.     (-1 == funcstr("a", "ab")) ? cout << "OK : 17" << " (" << -1 << " : " << (-1 == funcstr("a", "ab")) << " )" << endl : cout << "Failed : 17" << " (" << -1 << " : " << (-1 == funcstr("a", "ab")) << " )" << endl;
  79.     (-1 == funcstr("a", "bb")) ? cout << "OK : 18" << " (" << -1 << " : " << (-1 == funcstr("a", "bb")) << " )" << endl : cout << "Failed : 18" << " (" << -1 << " : " << (-1 == funcstr("a", "bb")) << " )" << endl;
  80.  
  81.  
  82.     (-1 == funcstr("a", "aaa")) ? cout << "OK : 19" << " (" << -1 << " : " << (-1 == funcstr("a", "aaa")) << " )" << endl : cout << "Failed : 19" << " (" << -1 << " : " << (-1 == funcstr("a", "aaa")) << " )" << endl;
  83.     (-1 == funcstr("aa", "aaa")) ? cout << "OK : 20" << " (" << -1 << " : " << (-1 == funcstr("aa", "aaa")) << " )" << endl : cout << "Failed : 20" << " (" << -1 << " : " << (-1 == funcstr("aa", "aaa")) << " )" << endl;
  84.     (0 == funcstr("aaa", "aaa")) ? cout << "OK : 21" << " (" << 0 << " : " << (0 == funcstr("aaa", "aaa")) << " )" << endl : cout << "Failed : 21" << " (" << 0 << " : " << (0 == funcstr("aaa", "aaa")) << " )" << endl;
  85.     (0 == funcstr("aaab", "aaa")) ? cout << "OK : 22" << " (" << 0 << " : " << (0 == funcstr("aaab", "aaa")) << " )" << endl : cout << "Failed : 22" << " (" << 0 << " : " << (0 == funcstr("aaab", "aaa")) << " )" << endl;
  86.     (1 == funcstr("baaa", "aaa")) ? cout << "OK : 23" << " (" << 1 << " : " << (1 == funcstr("baaa", "aaa")) << " )" << endl : cout << "Failed : 23" << " (" << 1 << " : " << (1 == funcstr("baaa", "aaa")) << " )" << endl;
  87.     (1 == funcstr("baaaa", "aaa")) ? cout << "OK : 24" << " (" << 1 << " : " << (1 == funcstr("baaaa", "aaa")) << " )" << endl : cout << "Failed : 24" << " (" << 1 << " : " << (1 == funcstr("baaaa", "aaa")) << " )" << endl;
  88.     (1 == funcstr("baaab", "aaa")) ? cout << "OK : 25" << " (" << 1 << " : " << (1 == funcstr("baaab", "aaa")) << " )" << endl : cout << "Failed : 25" << " (" << 1 << " : " << (1 == funcstr("baaab", "aaa")) << " )" << endl;
  89.     (-1 == funcstr("abd", "abc")) ? cout << "OK : 26" << " (" << -1 << " : " << (-1 == funcstr("abd", "abc")) << " )" << endl : cout << "Failed : 26" << " (" << -1 << " : " << (-1 == funcstr("abd", "abc")) << " )" << endl;
  90.  
  91.     (2 == funcstr("ababc", "abc")) ? cout << "OK : 27" << " (" << 2 << " : " << (2 == funcstr("ababc", "abc")) << " )" << endl : cout << "Failed : 27" << " (" << 2 << " : " << (2 == funcstr("ababc", "abc")) << " )" << endl;
  92.     (3 == funcstr("abdabc", "abc")) ? cout << "OK : 28" << " (" << 3 << " : " << (3 == funcstr("abdabc", "abc")) << " )" << endl : cout << "Failed : 28" << " (" << 3 << " : " << (3 == funcstr("abdabc", "abc")) << " )" << endl;
  93.  
  94. }
  95.  
  96.  
  97. int main()
  98. {
  99.    
  100.     std::string stroka;
  101.     std::string pattern;
  102.     std::cout << "Vvedite stroku :";
  103.     std::getline(std::cin, stroka);
  104.     std::cout << "Vvedite shablon:";
  105.     std::getline(std::cin, pattern);
  106.    
  107.     int res = funcstr(stroka.c_str(), pattern.c_str());
  108.    
  109.     //test();
  110.  
  111.     _getch();
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement