fix this c-language program

来源:百度知道 编辑:UC知道 时间:2024/05/22 12:45:51
#include <stdio.h>

int main(void)
{
int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0;

printf("Please enter a phrase:\n");

while((iochar=getchar())!=EOF)
{
if ((iochar=' ')||(iochar='\t')||(iochar='\n'))
{
numwhites++;
putchar(iochar);
}
else
if((iochar>='0')&&(iochar<='9'))
{
numdigits++;
putchar(iochar);
}
else
if(('a'<=iochar)&&(iochar<='z'))
{
numlower++;
putchar(iochar);
}
else
if(('A'<=iochar)&&(iochar<='Z'))
{
numupper++;
putchar(iochar);
}

fix:
if ((iochar==' ')||(iochar=='\t')||(iochar=='\n'))

手头没有C编译器,不能测试。
按打印文字看,
"xx lowercase have been converted to uppercase and ..." [xx 小写字母已被转换为大写字母]

你没有按要求把小写字母转换为大写字母再输出,只统计了个数。

// fix:
if(('a'<=iochar)&&(iochar<='z'))
{
numlower++;
putchar(iochar + ('A' - 'a') ); // 转换为大写字母输出
}

---------------------
题目没说清楚。phrase 为1行还是多行,如果是多行,结束标志是什么。
如果是1行,最好用 gets()读进1 行,再对一个一个 char 作上述分析统计。

如果是多行,从程序看用 EOF 作结束标志。如果是文件输入,文件结束就有EOF,从程序看,你用键盘输入,我不知道 怎样拍入 EOF, 能不能用 Ctrl+Z,没试验,不知道。

white spaces 至少还要包括 '\r'.