#include <stdio.h>
main()
{
char key; /* Holds the current input character */
int count = 0; /* Number of "the" encountered */
int match = 0; /* Number of previous characters that match */
/* When the user types a newline, the program will end */
while ((key = getchar()) != '\n') {
switch (match) {
case 0: /* Starting point */
if (key == 't')
match++;
else
match = 0;
break;
case 1: /* Got a 't' already. If this character */
if (key == 'h') /* is an 'h', then proceed to 'e'. */
match++;
else if (key == 't') /* If it is a 't', start from match = 1 */
match = 1;
else /* otherwise start over */
match = 0;
break;
case 2: /* Got 'th' already */
if (key == 'e') { /* if we get an 'e', increment */
count++; /* and start over. */
match = 0;
}
else if (key == 't') /* If we get a 't' start from match = 1 */
match = 1;
else /* else start over. */
match = 0;
break;
}
}
printf("The number of times 'the' was detected: %d\n", count);
}