/*--------------------------------------------------------------------*/ /* upper5.c */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #include #include enum StateType {NORMAL, INWORD}; /*--------------------------------------------------------------------*/ enum StateType handleNormalState(int iChar) /* Implement the NORMAL state of the DFA. iChar is the current DFA character. Return the next state. */ { enum StateType eState; if (isdigit(iChar)) { putchar(iChar); eState = INWORD; } else if (isupper(iChar)) { putchar(iChar); eState = INWORD; } else if (islower(iChar)) { putchar(toupper(iChar)); eState = INWORD; } else { putchar(iChar); eState = NORMAL; } return eState; } /*--------------------------------------------------------------------*/ enum StateType handleInwordState(int iChar) /* Implement the INWORD state of the DFA. iChar is the current DFA character. Return the next state. */ { enum StateType eState; if (isalnum(iChar)) { putchar(iChar); eState = INWORD; } else { putchar(iChar); eState = NORMAL; } return eState; } /*--------------------------------------------------------------------*/ int main(void) /* Read text from stdin. Convert the first character of each "word" to uppercase, where a word is a sequence of alphanumeric characters. Write the result to stdout. Return 0. */ /* Use a DFA approach. */ { int iChar; enum StateType eState = NORMAL; while ((iChar = getchar()) != EOF) { switch (eState) { case NORMAL: eState = handleNormalState(iChar); break; case INWORD: eState = handleInwordState(iChar); break; } } return 0; }