Advertisement
Cinder1986

KT1 C

Dec 4th, 2022
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | Food | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef enum charType
  5. {
  6.     ctOther,
  7.     ctQout
  8. } charType;
  9.  
  10. int transitions[4][2] =
  11. {
  12.     {0, 0},
  13.     {0, 2},
  14.     {2, 3},
  15.     {0, 2}
  16. };
  17.  
  18. int isFinalState[4] = { 0, 0, 0, 1 };
  19.  
  20. charType getCharType(char c)
  21. {
  22.     switch (c)
  23.     {
  24.     case '\'':
  25.         return ctQout;
  26.         break;
  27.     default:
  28.         return ctOther;
  29.         break;
  30.     }
  31. }
  32.  
  33. int checkString(char* s)
  34. {
  35.     int state = 1;
  36.     int i = 0;
  37.     while (*s)
  38.     {
  39.         state = transitions[state][getCharType(s[0])];
  40.         *s++;
  41.     }
  42.     return isFinalState[state];
  43. }
  44.  
  45. int printSubStrings(char* s)
  46. {
  47.     int i = 1;
  48.     int counter = 0;
  49.     while (*(s + 1))
  50.     {
  51.         if (i > 0 && *s == '\'' && *(s + 1) == '\'')
  52.         {
  53.             printf("\n%d and %d symbols\n", i, i + 1);
  54.             i += 2;
  55.             *s += 2;
  56.             counter++;
  57.         }
  58.         else
  59.         {
  60.             i++;
  61.             *s++;
  62.         }
  63.     }
  64.     return counter;
  65. }
  66.  
  67. int main()
  68. {
  69.     char s[100];
  70.     gets(s);
  71.     int isCorrect = checkString(s);
  72.     if (isCorrect)
  73.     {
  74.         printf("\nThe string is correct\n");
  75.         int subStringCount = printSubStrings(s);
  76.         subStringCount ? printf("\nThe string contains %d required substrings\n", subStringCount) : printf("\nThe string does not contain required substrings\n");
  77.     }
  78.     else
  79.         printf("The string is not correct\n");
  80.     getchar();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement