Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char mytoLower(char myString)
  5. {
  6.     if ((myString >= 'A') && (myString <= 'Z'))
  7.     {
  8.         return myString - 'A' + 'a';
  9.     }
  10.     else
  11.     {
  12.         return myString;
  13.     }
  14. }
  15.  
  16. int strLen(const char*myString)
  17. {
  18.     if (!*myString)
  19.     {
  20.         return 0;
  21.     }
  22.  
  23.     return 1 + strLen(myString + 1);
  24. }
  25.  
  26. int timesFound(const char*myString, const char*what, int stringLength, int whatLength)
  27. {
  28.     int result = 0;
  29.     int checker = 0;
  30.     int i = 0;
  31.     int k = 0;
  32.  
  33.     for (; i < stringLength; i++)
  34.     {
  35.         for (; k < whatLength; )
  36.         {
  37.             if (checker == whatLength)
  38.             {
  39.                 result++;
  40.                 checker = 0;
  41.                 k = 0;
  42.                 break;
  43.             }
  44.  
  45.             if (mytoLower(myString[i]) ==mytoLower(what[k]))
  46.             {
  47.                 k++;
  48.                 checker++;
  49.                 if (checker == whatLength)
  50.                 {
  51.                     result++;
  52.                     checker = 0;
  53.                     k = 0;
  54.                     break;
  55.                 }
  56.  
  57.                 break;
  58.             }
  59.             else
  60.             {
  61.                 checker = 0;
  62.                 k = 0;
  63.                 break;
  64.             }
  65.         }
  66.     }
  67.  
  68.     return result;
  69. }
  70.  
  71. int main()
  72. {
  73.     const int MAX_SIZE = 1024;
  74.     int withSize, whatSize, stringSize,newStringLength;
  75.     char*string = new(nothrow) char[MAX_SIZE];
  76.  
  77.     if (!string)
  78.     {
  79.         return 1;
  80.     }
  81.  
  82.     cin.getline(string, MAX_SIZE);
  83.     const char*myString = string;
  84.     char*what = new(nothrow) char[MAX_SIZE];
  85.  
  86.     if (!what)
  87.     {
  88.         return 1;
  89.     }
  90.  
  91.     cin.getline(what, MAX_SIZE);
  92.     const char*myWhat = what;
  93.     char*with = new(nothrow) char[MAX_SIZE];
  94.  
  95.     if (!with)
  96.     {
  97.         return 1;
  98.     }
  99.  
  100.     cin.getline(with, MAX_SIZE);
  101.  
  102.     const char*myWith = with;
  103.     withSize = strLen(myWith);
  104.     whatSize = strLen(myWhat);
  105.     stringSize = strLen(myString);
  106.  
  107.     newStringLength = stringSize + (withSize-whatSize)*timesFound(myString, what, stringSize, whatSize);
  108.  
  109.     cout << withSize<<" with\n";
  110.     cout << whatSize << " what\n";
  111.     cout << stringSize << " string\n";
  112.     cout << newStringLength;
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement