Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int cointainsNano(const char* str)
- {
- int state = 0;
- while (*str)
- {
- if (state == 0)
- {
- if (*str == 'n')
- {
- state = 1;
- }
- }
- else if (state == 1)
- {
- if (*str == 'a')
- {
- state = 2;
- }
- else if (*str == 'n')
- {
- }
- else
- {
- state = 0;
- }
- }
- else if (state == 2)
- {
- if (*str == 'n')
- {
- state = 3;
- }
- else
- {
- state = 0;
- }
- }
- else if (state == 3)
- {
- if (*str == 'o')
- {
- state = 4;
- return 1;
- }
- else if (*str == 'n')
- {
- state = 1;
- }
- else if (*str == 'a')
- {
- state = 2;
- }
- else
- {
- state = 0;
- }
- }
- str++;
- }
- return 0;
- }
- int countNano(const char* str)
- {
- int cnt = 0;
- int state = 0;
- while (*str)
- {
- if (state == 0)
- {
- if (*str == 'n')
- {
- state = 1;
- }
- }
- else if (state == 1)
- {
- if (*str == 'a')
- {
- state = 2;
- }
- else if (*str == 'n')
- {
- }
- else
- {
- state = 0;
- }
- }
- else if (state == 2)
- {
- if (*str == 'n')
- {
- state = 3;
- }
- else
- {
- state = 0;
- }
- }
- else if (state == 3)
- {
- if (*str == 'o')
- {
- state = 4;
- cnt++;
- }
- else if (*str == 'n')
- {
- state = 1;
- }
- else if (*str == 'a')
- {
- state = 2;
- }
- else
- {
- state = 0;
- }
- }
- else if (state == 4)
- {
- if (*str == 'n')
- {
- state = 1;
- }
- else
- {
- state = 0;
- }
- }
- str++;
- }
- return cnt;
- }
- int main()
- {
- char str1[50];
- printf("Enter string: ");
- gets(str1);
- printf("Contains nano: %s\n", cointainsNano(str1) ? "yes" : "no");
- printf("nano contains %d times\n", countNano(str1));
- return 0;
- }
Add Comment
Please, Sign In to add comment