有一篇文章,共有3行文字,每行有80个字符。编写程序分别统计出文章中英文大写字母、小写字母、中文字符、数字、空格及其他字符的个数。
中文字符统计不对
unsigned char a[3][81];
int cnt_upper = 0, cnt_lower = 0, cnt_chinese = 0, cnt_digit = 0, cnt_space = 0, cnt_others = 0;
gets( a[0] );
gets( a[1] );
gets( a[2] );
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < strlen( a[i] ); j++ )
{
if ( a[i][j] >= 'a' && a[i][j] <= 'z' )
cnt_lower++;
else if ( a[i][j] >= 'A' && a[i][j] <= 'Z' )
cnt_upper++;
else if ( a[i][j] >= '0' && a[i][j] <= '9' )
cnt_digit++;
else if ( a[i][j] == ' ' )
cnt_space++;
else if ( a[i][j] >= 128 && a[i][j + 1] >= 128 )
{
cnt_chinese++, j++;
}else cnt_others++;
}
}
printf( "upper:%d\nlower:%d\nchinese:%d\ndigit:%d\nspace:%d\nothers:%d\n", cnt_upper, cnt_lower, cnt_chinese, cnt_digit, cnt_space, cnt_others );
return(0);
输入
I like 编程(重复三遍)
预期输出
upper:3 lower:12 chinese:6 digit:0 space:6 others:0
实际输出
upper:3 lower:12 chinese:9 digit:0 space:6 others:0
不知道哪出错了
我想要达到的结果
改为如下:
char a[3][81];
int cnt_upper = 0, cnt_lower = 0, cnt_chinese = 0, cnt_digit = 0, cnt_space = 0, cnt_others = 0;
gets(a[0]);
gets(a[1]);
gets(a[2]);
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < strlen( a[i] ); j++ )
{
if ( a[i][j] >= 'a' && a[i][j] <= 'z' )
cnt_lower++;
else if ( a[i][j] >= 'A' && a[i][j] <= 'Z' )
cnt_upper++;
else if ( a[i][j] >= '0' && a[i][j] <= '9' )
cnt_digit++;
else if ( a[i][j] == ' ' )
cnt_space++;
else if ( a[i][j] < 0 )
{
cnt_chinese++, j++;
}else cnt_others++;
}
}
printf( "upper:%d\nlower:%d\nchinese:%d\ndigit:%d\nspace:%d\nothers:%d\n", cnt_upper, cnt_lower, cnt_chinese, cnt_digit, cnt_space, cnt_others );
return 0;