I'm in my first programming class and having trouble with a project of ours. The program is designed to take a string's inputs and see if they match the pattern and recognize if the pattern is broken; in this case it is meant to recognize if the user inputs "hahaha!", "hohohoho!", or a mixture of the two 'ha' and 'ho' (always ending in '!').
My trouble is that I have started an attempt at this code using switch
case
s, but do not know if this is the most effective way to program for the project, or if it is even possible to do it this way.
Here is my code so far, please help me.
#include <stdio.h>
#include <string.h>
#define string_len 100
int main ()
{
char string[string_len];
int state = 0;
while(1)
{
for(int i = 0; i < string_len; i++)
{
printf("Hello, I can tell if you are laughing or not, you can exit by typing 'bye': \n");
scanf("%s", string);
for(state = 0; state < 5;)
{
switch(state)
{
case 0:
if(strcmp(string, "bye") == 0)
printf("Bye now!\n");
return 0;
break;
case 1:
if(string[i] == 'h')
state++;
else(printf("you are not laughing\n"));
break;
case 2:
if(string[i] == 'a')
state--;
else(state++);
break;
case 3:
if(string[i] == 'o')
state = state-2;
else(printf("you are not laughing\n"));
break;
case 4:
if(string[i] == '!')
printf("You are laughing!\n");
else(printf("You are not laughing\n"));
}
}
}
return 0;
}
}
I think that I may be mixed up with the state part of my program in the switch. I'm trying to allow it to go from:
state 0 : check if it says bye, if so "bye now"
state 1: is it an h? if so, check for a or o, if not "you arent laughing"
state 2: is it an a? if so, check for an 'h' or '!' -This is where I'm especially confused, if not is it an o?
state 3: is it an o? if so, check for an 'h' or '!', if not "you aren't laughing"
state 4: is it an '!'? if so "you are laughing" if not, "you are not laughing"
I hope I have formatted this question well enough, please let me know if I could make this more readable in any way and if you have any questions for me.
Thank you all for the help.