I have this assignment:
Write a program that reads input up to # and reports the number of times that the sequence ei occurs.
I was wondering which method of the two below is preferred (red or green). They both tested fine, but is one better than the other? Would one be faster than the other, or is one more stylistic for programming in c? I prefer the latter, but I don't have any reason why.
I don't have a class and it's not for a grade, I'm just curious. thanks for any input
Code:
#include<stdio.h>
#include<stdbool.h>
int main(void) {
bool e = false;
int c;
int count = 0;
printf("Enter something\n");
while ((c = getchar()) != '#') {
/*switch (c) {
case 'e':
e = true;
continue;
case 'i' :
if (e)
count++;
default :
e = false;
}*/
if (c == 'e')
e = true;
else if (c == 'i') {
if (e)
count++;
e = false;
}
else
e = false;
}
printf("You have %d %s of \"ei\"\n,", count,
(count == 1) ? "grouping" : "groupings");
return 0;
}