#include <stdio.h>
int main() { int transitionTable[3][2] = { //input: 0 1 /* q0 */{0,1}, /* q1 */{0,2}, /* q2 */{3,1} };
char buf[256];
printf("Please enter a binary number: ");
scanf("%s", buf);
int state = 0; //initial state int value; char* pChar = buf; while (state != 3 && *pChar != '\0') { value = (int)(*pChar - '0'); //convert the char into an int valued 0 or 1
if (value < 0 || value > 1) { printf("Number must be binary !"); return 0; }
state = transitionTable[state][value]; //state transition pChar++; }
if (state == 3) { printf("The sequence 110 was found in the binary number.\n"); } else { printf("The sequence 110 was NOT found in the binary number.\n"); }
return 0; }
|