注册 登录
编程论坛 C++教室

谁能教我这个基础编程

orleans 发布于 2010-04-06 03:11, 429 次点击
读取一个文件夹的每个字母
output 是每个字母出现的次数
比如说,文件里是:"how are you"
就是 h:1 o:2 w:1 a:1 r:1........
然后还得计算write space和uppercase and lower case letters

谢谢大侠
2 回复
#2
cnfarer2010-04-06 14:07
先建个数组用于存放各字符的个数
再顺序读字符,相应的数据元素加1,读下一个字符。。。(用循环处理)
最后统计一下就行了
#3
apull2010-04-06 20:59
直接写了,大概就这个思路。
程序代码:
#include <iostream>
using namespace std;

int main()
{
char ch[128];
char str[255];
int w=0;u=0;l=0;
memset(ch,0,128);
cin>>str;
int asc;
for(int i=0;str[i]!='\0';++i)
{
asc=str[i];
ch[asc]++;
if (asc>='A' && asc<='Z')++u;
if(asc>='a' && asc<='z')++l;
if(asc==32)++w;
}
cout <<"w:" << w <<endl;
cout <<"l:" << l << endl;
cout <<"u:" << u << endl;
for(int i=32;i<128;++i)
if(ch[i]>0)cout << (char)i  << "有:" << ch[i]<< endl;
}
1