太空工程师 焊接 完成:关于C的问题~!!?

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 11:16:52
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

同意楼上的,给你个参考:
利用while循环,条件为输入的字符不为'\n'
#include "stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
}

#include <iostream.h>
#include <ctype.h>

const long MAX = 1000;

int main()
{
char str[MAX];
long digitNumber=0, letterNumber=0, blankNumber=0, otherCharNumber=0;
long loopCount;

while(cin.getline(str,MAX), cin.rdstate() == cin.goodbit)
{
for(loopCount = 0; str[loopCount] != '\0'; loopCount ++)
{
if(isalpha(str[loopCount]) != 0)
letterNumber ++;
else if(isdigit(str[loopCount]) != 0)
digitNumber ++;
else if(str[loopCount] == ' ')
blankNumber ++;
else
otherCharNumber ++;
}
}

cout << "There are " << letterNumber << " letters!" << endl
<< "There are " << digitNumber << " digits!" << endl
<< "There are " << blankNumber << " blanks!" << endl
<< "There are " << otherCharNumber << " other characters!" << endl;
return 0;
}
C++写的,自己改一下就是C了的

你应该先自己试着写,然后调试,仍然搞不懂,再贴上来问,