Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- typedef enum charType
- {
- ctOther,
- ctQout
- } charType;
- int transitions[4][2] =
- {
- {0, 0},
- {0, 2},
- {2, 3},
- {0, 2}
- };
- int isFinalState[4] = { 0, 0, 0, 1 };
- charType getCharType(char c)
- {
- switch (c)
- {
- case '\'':
- return ctQout;
- break;
- default:
- return ctOther;
- break;
- }
- }
- int checkString(char* s)
- {
- int state = 1;
- int i = 0;
- while (*s)
- {
- state = transitions[state][getCharType(s[0])];
- s++;
- }
- return isFinalState[state];
- }
- char* strCopy(char* s, int from, int to)
- {
- char* buffer = malloc((to - from) * sizeof(char));
- int i = 0;
- while (from + i <= to)
- buffer[i] = s[from + i++];
- buffer[i] = '\0';
- return buffer;
- }
- int countAllQuots(char *s)
- {
- int i = 0;
- int count = 0;
- while (*(s + i))
- {
- if (*(s + i) == '\'')
- count++;
- i++;
- }
- return count;
- }
- int* getAllQuots(char* s, int quots)
- {
- int* quotArr = malloc(quots * sizeof(int));
- int i = 0;
- int j = 0;
- while (*(s + i))
- *(s + i) == '\'' ? quotArr[j++] = i++ : i++;
- return quotArr;
- }
- char* cutExtras(char* s)
- {
- int j = strlen(s);
- int i = 0;
- while (*(s + i + 1) && (*(s + i) != '\'' || (*(s + i) == '\'' && *(s + i + 1) == '\'')))
- i++;
- while (j > 0 && (*(s + j) != '\'' || (*(s + j) == '\'' && *(s + j - 1) == '\'')))
- j--;
- return strCopy(s, i, j);
- }
- void printWordsStrings(char* s, int* arr, int size)
- {
- int start = 0;
- int i = 1;
- while (start < size - 1)
- {
- while (start < size - i)
- {
- char* part = strCopy(s, arr[start], arr[size - i]);
- if (checkString(part) && strlen(part) > 0)
- printf("%s\n", part);
- else
- {
- part = cutExtras(part);
- if (checkString(part) && strlen(part) > 0)
- printf("%s\n", part);
- }
- i += 2;
- }
- printf("\n");
- i = 1;
- start += 2;
- }
- }
- void printEmptyStrings(int* arr, int size)
- {
- int count = 0;
- for (int i = 0; i < size - 2; i++)
- if (arr[i + 1] - arr[i] == 1)
- count++;
- if (arr[size - 1] - arr[size - 2] == 1)
- count++;
- printf("%d empty strings\n\n", count);
- }
- void printSubstrings(char* s, int* arr, int size)
- {
- printWordsStrings(s, arr, size);
- printEmptyStrings(arr, size);
- }
- // aaa'bbbb''cccc''vvvv''ccc''bbbbb'
- // hhhh'ggggf'''hhh''jjj''kkkk'
- int main()
- {
- printf("Enter a string: ");
- char s[100] = "";
- gets(s);
- int choice, isRunning = 1;
- do
- {
- printf("1.Check the string\n2.Find all substrings\n3.Exit\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- checkString(s) ? printf("The string is correct\n") : printf("The string is not correct\n");
- break;
- case 2:
- {
- int quots = countAllQuots(s);
- int* quotArr = getAllQuots(s, quots);
- printSubstrings(s, quotArr, quots);
- }
- break;
- case 3:
- isRunning = 0;
- break;
- default:
- printf("Wrong option\n");
- }
- } while (isRunning);
- return 0;
- }
Add Comment
Please, Sign In to add comment