写一个程序,输入一个字符串,统计数字、空格、字母和其它字符出现的次数。

如题所述

#include <stdio.h>
main()
{
int zm=0,kg=0,shz=0,qt=0;
char c;
printf("请输入字符:\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
zm++;
else if(c==' ')
kg++;
else if(c>='0'&&c<='9')
shz++;
else
qt++;
}
printf("字母=%d\n空格=%d\n数字=%d\n其它=%d\n",zm,kg,shz,qt);
}
用C语言编写的,我运行过.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-04-28
给你个大致的思想,具体自己编吧,这样才会有进步!

main()
{int i=0,sz=0,zm=0,kg=0,qt=0;
char s[100];
s=gets(s);
while(s[i]!='\0')
{
if(字符a的ASCII<s[i]<字符z的ASCII||字符A的 ASCII<s[i]<字符Z的ASCII)
zm++;
else if(9的ASCII<s[i]<0的ASCII )
sz++;
else if(s[i]==空格的ASCII )
kg++;
else
qt++;
}
printf('数字');
printf('字母');
printf('空格');
printf('其他');

}

}本回答被提问者采纳
第2个回答  2007-04-28
#include <stdio.h>
#include <string.h>

int main()
{
int i, d, s, t, k;
char str[255];
printf("请输入一个字符串");
gets(str);
d = s = t = k = 0;
for (i = 0;i < strlen(str);i++)
{
if (str[i] >= '0' && str[i] <= '9') d++;
else if ((str[i] >= 'a' && str[i] <= 'z')
|| (str[i] >= 'A' && str[i] <= 'Z')) s++;
else if (str[i]==' ') k++;
else t++;
}
printf("数字:%d,字母:%d,空格:%d,其它:%d", d, s, k, t);
getch();
return 0;
}
第3个回答  2007-04-28
#include <iostream>
using namespace std;
int main()
{int letters=1,space=0,digit=0,other=0;
char ch; //letters是英文个数,space空格数,digit数字数
cout<<"输入一串字符:";
cin>>ch;
while((ch=getchar())!='\n')
{if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{letters=letters+1;}
else if(ch==' ') //一个空格
{space=space+1;}
else if(ch>='0'&&ch<='9')
{digit=digit+1;}
else {other=other+1;}
}
cout<<"leters="<<letters<<",space="<<space\
<<",digit="<<digit<<",other="<<other<<endl;
return 0;
}
第4个回答  2007-04-28
用什么去写啊,
不要告诉我用delphi
我就看过一本书上有这个例子
相似回答