Cinder1986

KT1 C2

Dec 4th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | Food | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5.  
  6. typedef enum charType
  7. {
  8. ctOther,
  9. ctQout
  10. } charType;
  11.  
  12. int transitions[4][2] =
  13. {
  14. {0, 0},
  15. {0, 2},
  16. {2, 3},
  17. {0, 2}
  18. };
  19.  
  20. int isFinalState[4] = { 0, 0, 0, 1 };
  21.  
  22. charType getCharType(char c)
  23. {
  24. switch (c)
  25. {
  26. case '\'':
  27. return ctQout;
  28. break;
  29. default:
  30. return ctOther;
  31. break;
  32. }
  33. }
  34.  
  35. int checkString(char* s)
  36. {
  37. int state = 1;
  38. int i = 0;
  39. while (*s)
  40. {
  41. state = transitions[state][getCharType(s[0])];
  42. s++;
  43. }
  44. return isFinalState[state];
  45. }
  46.  
  47. char* strCopy(char* s, int from, int to)
  48. {
  49. char* buffer = malloc((to - from) * sizeof(char));
  50. int i = 0;
  51. while (from + i <= to)
  52. buffer[i] = s[from + i++];
  53. buffer[i] = '\0';
  54. return buffer;
  55. }
  56.  
  57. int countAllQuots(char *s)
  58. {
  59. int i = 0;
  60. int count = 0;
  61. while (*(s + i))
  62. {
  63. if (*(s + i) == '\'')
  64. count++;
  65. i++;
  66. }
  67. return count;
  68. }
  69.  
  70. int* getAllQuots(char* s, int quots)
  71. {
  72. int* quotArr = malloc(quots * sizeof(int));
  73. int i = 0;
  74. int j = 0;
  75. while (*(s + i))
  76. *(s + i) == '\'' ? quotArr[j++] = i++ : i++;
  77. return quotArr;
  78. }
  79.  
  80. char* cutExtras(char* s)
  81. {
  82. int j = strlen(s);
  83. int i = 0;
  84. while (*(s + i + 1) && (*(s + i) != '\'' || (*(s + i) == '\'' && *(s + i + 1) == '\'')))
  85. i++;
  86. while (j > 0 && (*(s + j) != '\'' || (*(s + j) == '\'' && *(s + j - 1) == '\'')))
  87. j--;
  88. return strCopy(s, i, j);
  89. }
  90.  
  91. void printWordsStrings(char* s, int* arr, int size)
  92. {
  93. int start = 0;
  94. int i = 1;
  95. while (start < size - 1)
  96. {
  97. while (start < size - i)
  98. {
  99. char* part = strCopy(s, arr[start], arr[size - i]);
  100. if (checkString(part) && strlen(part) > 0)
  101. printf("%s\n", part);
  102. else
  103. {
  104. part = cutExtras(part);
  105. if (checkString(part) && strlen(part) > 0)
  106. printf("%s\n", part);
  107. }
  108. i += 2;
  109. }
  110. printf("\n");
  111. i = 1;
  112. start += 2;
  113. }
  114. }
  115.  
  116. void printEmptyStrings(int* arr, int size)
  117. {
  118. int count = 0;
  119. for (int i = 0; i < size - 2; i++)
  120. if (arr[i + 1] - arr[i] == 1)
  121. count++;
  122. if (arr[size - 1] - arr[size - 2] == 1)
  123. count++;
  124. printf("%d empty strings\n\n", count);
  125. }
  126.  
  127. void printSubstrings(char* s, int* arr, int size)
  128. {
  129. printWordsStrings(s, arr, size);
  130. printEmptyStrings(arr, size);
  131. }
  132.  
  133. // aaa'bbbb''cccc''vvvv''ccc''bbbbb'
  134. // hhhh'ggggf'''hhh''jjj''kkkk'
  135.  
  136. int main()
  137. {
  138. printf("Enter a string: ");
  139. char s[100] = "";
  140. gets(s);
  141. int choice, isRunning = 1;
  142. do
  143. {
  144. printf("1.Check the string\n2.Find all substrings\n3.Exit\n");
  145. scanf("%d", &choice);
  146. switch (choice)
  147. {
  148. case 1:
  149. checkString(s) ? printf("The string is correct\n") : printf("The string is not correct\n");
  150. break;
  151. case 2:
  152. {
  153. int quots = countAllQuots(s);
  154. int* quotArr = getAllQuots(s, quots);
  155. printSubstrings(s, quotArr, quots);
  156. }
  157. break;
  158. case 3:
  159. isRunning = 0;
  160. break;
  161. default:
  162. printf("Wrong option\n");
  163. }
  164. } while (isRunning);
  165. return 0;
  166. }
Add Comment
Please, Sign In to add comment