Code:/* * * Purpose: Determine if a string of 0's and/or 1's ends in 00 or 11. * * Input: String of n 0's or 1's terminated with a newline * * * Output: Message: * * Accepted (if the string ends in 00 or 11), state = (accept state) # * -or- * Rejected (if the string does not end in 00 or 11), state (reject state) # * * Caveat: If the input contains other than 0's and/or 1's, the result (output) * undefined! * * Return Values: Subject to the above caveat, if the return value is: * * 0: No input * 1: The last two characters were 10 * 2: The last two characters were 01 * 3: The last two characters were 00 * 4: The last two characters were 11 * */ #include <stdio.h> main() { int state, n=1; char input; for(;n==1;) { state=0; printf("Enter a string of 0's and 1's (e.g. 101011) followed newline\n\n"); while ( (input=getchar()) != '\n' ) { /* State 0 */ if ( state == 0 ) if ( input == '0' ) state=1; else state = 2; else /* State 1 */ if ( state == 1 ) if ( input == '0' ) state = 3; else state = 2; else /* State 2 */ if ( state == 2 ) if ( input == '0' ) state = 1; else state = 4; else /* State 3 */ if ( state == 3 ) { if ( input == '1' ) state = 2; } else /* State 4 */ if ( state == 4 ) { if ( input =='0' ) state = 1; } } if ( state > 2 ) printf("Accepted, state = %i\n", state); else printf("Rejected, state = %i\n", state); } }
I want to turn these if-else statements to switch statements/conditional expressions but i'm having a little bit of trouble.
Any help?