Vasilena

PracticeDayFiveExercises5,6

Jul 9th, 2021 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int cointainsNano(const char* str)
  5. {
  6.     int state = 0;
  7.     while (*str)
  8.     {
  9.         if (state == 0)
  10.         {
  11.             if (*str == 'n')
  12.             {
  13.                 state = 1;
  14.             }
  15.         }
  16.         else if (state == 1)
  17.         {
  18.             if (*str == 'a')
  19.             {
  20.                 state = 2;
  21.             }
  22.             else if (*str == 'n')
  23.             {
  24.  
  25.             }
  26.             else
  27.             {
  28.                 state = 0;
  29.             }
  30.         }
  31.         else if (state == 2)
  32.         {
  33.             if (*str == 'n')
  34.             {
  35.                 state = 3;
  36.             }
  37.             else
  38.             {
  39.                 state = 0;
  40.             }
  41.         }
  42.         else if (state == 3)
  43.         {
  44.             if (*str == 'o')
  45.             {
  46.                 state = 4;
  47.                 return 1;
  48.             }
  49.             else if (*str == 'n')
  50.             {
  51.                 state = 1;
  52.             }
  53.             else if (*str == 'a')
  54.             {
  55.                 state = 2;
  56.             }
  57.             else
  58.             {
  59.                 state = 0;
  60.             }
  61.         }
  62.         str++;
  63.     }
  64.     return 0;
  65. }
  66.  
  67. int countNano(const char* str)
  68. {
  69.     int cnt = 0;
  70.     int state = 0;
  71.     while (*str)
  72.     {
  73.         if (state == 0)
  74.         {
  75.             if (*str == 'n')
  76.             {
  77.                 state = 1;
  78.             }
  79.         }
  80.         else if (state == 1)
  81.         {
  82.             if (*str == 'a')
  83.             {
  84.                 state = 2;
  85.             }
  86.             else if (*str == 'n')
  87.             {
  88.  
  89.             }
  90.             else
  91.             {
  92.                 state = 0;
  93.             }
  94.         }
  95.         else if (state == 2)
  96.         {
  97.             if (*str == 'n')
  98.             {
  99.                 state = 3;
  100.             }
  101.             else
  102.             {
  103.                 state = 0;
  104.             }
  105.         }
  106.         else if (state == 3)
  107.         {
  108.             if (*str == 'o')
  109.             {
  110.                 state = 4;
  111.                 cnt++;
  112.             }
  113.             else if (*str == 'n')
  114.             {
  115.                 state = 1;
  116.             }
  117.             else if (*str == 'a')
  118.             {
  119.                 state = 2;
  120.             }
  121.             else
  122.             {
  123.                 state = 0;
  124.             }
  125.         }
  126.         else if (state == 4)
  127.         {
  128.             if (*str == 'n')
  129.             {
  130.                 state = 1;
  131.             }
  132.             else
  133.             {
  134.                 state = 0;
  135.             }
  136.         }
  137.         str++;
  138.     }
  139.     return cnt;
  140. }
  141.  
  142. int main()
  143. {
  144.     char str1[50];
  145.     printf("Enter string: ");
  146.     gets(str1);
  147.     printf("Contains nano: %s\n", cointainsNano(str1) ? "yes" : "no");
  148.     printf("nano contains %d times\n", countNano(str1));
  149.     return 0;
  150. }
Add Comment
Please, Sign In to add comment