Page 65 - Programming Microcontrollers in C
P. 65
50 Chapter 1 Introduction to C
int na=0,ne=0,ni=0,no=0,nu=0;
int nother=0,c;
while ((c=getchar())!=EOF)
switch( c)
{
case ‘A’:
case ‘a’: na=na+1;
break;
case ‘E’:
case ‘e’: ne=ne+1;
break;
case ‘I’:
case ‘i’: ni=ni+1;
break;
case ‘O’:
case ‘o’: no=no+1;
break;
case ‘U’:
case ‘u’: nu=nu+1;
break;
default:
nother=nother+1;
}
printf(“As=%d, Es=%d, Is=%d, Os=%d, Us=%d and”
“ Others=%d\n” ,na,ne,ni,no,nu,nother);
return 0;
}
This program performs exactly the same function as the earlier one.
The data are read in a character at a time as before. Here, however,
the switch statement is used. The statement switch(c) causes the
argument of the switch to be compared with the constants follow
ing each of the case statements that follows. When a match occurs,
the next set of statements to follow a colon will be executed. Once
the program starts to execute statements, all of the following state
ments will be executed unless the programmer does something to
cause the program to be redirected. The break instruction does ex