Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- int Automat(int c, int status, int *match);
- int main()
- {
- FILE *input;
- int c;
- int status = 0;
- int match = 0;
- char fName[32];
- int count = 0;
- printf("Enter name: ");
- scanf("%s", &fName);
- if (!(input = fopen(fName, "r")))
- {
- printf("The file does not exist.\n");
- system("pause");
- exit(1);
- }
- while ((c = fgetc(input)) != EOF)
- {
- status = Automat(c, status, &match);
- if (match == 2)
- {
- count++;
- match = 0;
- }
- }
- printf("Found matches: %d\n", count);
- system("pause");
- return 0;
- }
- int Automat(int c, int status, int *match)
- {
- switch (status)
- {
- case 0:
- if (c == 'a')
- {
- *match = 1;
- status = 1;
- }
- else
- {
- *match = 0;
- status = 0;
- }
- break;
- case 1:
- if (c == 'b')
- {
- *match = 2;
- status = 0;
- }
- else
- {
- *match = 0;
- status = 0;
- }
- break;
- }
- return status;
- }
Advertisement
Add Comment
Please, Sign In to add comment