数组练习
//test_5
//统计各个数字,空白符(空格符,制表符,换行符),其他字符出现的次数
#include <stdio.h>
int main()
{
char c;
int i;
int nwhite = 0, nother = 0;
int ndigit[10] = {0}; //数组
while( ( c = getchar() ) != EOF ) {
if ( c >= '0' && c <= '9' ) {
ndigit[c-'0']++; //数字由char转换成int -'0'
}
else if ( c == ' ' || c == '\t' || c == '\n' ) {
nwhite++;
}
else {
nother++;
}
if ( c == '.' )
break;
}
printf( "ndigit =" );
for (i = 0; i <= 9; i++)
printf (" %d", ndigit[i]);
printf(", nwhite = %d, nother = %d.\n", nwhite, nother);
return 0;
}
多路判定
- if...else...
- if (条件1)
- 语句1;
- else if(条件2)
- 语句2;
- ...
- else
- 语句n;
- if (条件1)