-
Notifications
You must be signed in to change notification settings - Fork 10
/
2_b_abba.c
54 lines (49 loc) · 1.06 KB
/
2_b_abba.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
int main()
{
char str[20];
int i = 0, state = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[i] != '\0')
{
switch (state)
{
case 0:
if (str[i] == 'a')
state = 1;
else
state = 5;
break;
case 1:
if (str[i] == 'b')
state = 2;
else
state = 5;
break;
case 2:
if (str[i] == 'b')
state = 3;
else
state = 5;
break;
case 3:
if (str[i] == 'a')
state = 4;
else
state = 5;
break;
case 4:
state = 5; // Any additional characters after 'abba' will lead to invalid state
break;
case 5:
break; // Invalid state
}
i++;
}
if (state == 4)
printf("The string is accepted\n");
else
printf("The string is invalid\n");
return 0;
}