c++ 输入一串字符,统计各字符出现的次数(分为大写字母、小写字母、数字)

1.编写菜单完成下列的计算功能:
(1)请编写在字符串S1查找字符串S2出现次数的函数;
(2)输入一串字符,统计各字符出现的次数(分为大写字母、小写字母、数字);
(3)将数字编号“翻译”成英文编号。例如:将编号35706“翻译”成英文编号为:three-five-seven-zero-six
最好每一步程序注解下

回答得好的话会再加分。。。。。谢谢咯
对不起咯,希望能用C语言的方法来做程序。。。

希望对你有些帮助。
#include "stdafx.h"
#include "iostream.h"
#define N 40
/* 字符串匹配函数 */
void Comparison(char *s1,char *s2)
{
int i=0,j,m;
while(*s1!='\0')
{
m=1;j=0;
if(*s1==*s2)
for(;*s2!='\0';s2++) //s2中字符逐个比较
{
j++; //记录指针偏移值
if(*s1==*s2) s1++;
else m=0 ; //作为后面两字符串不同的判断
}
if(m!=0&&*s2=='\0') i++;
s1=s1-j+1; //从下一个字符开始
s2=s2-j; //指针回到原来的位置
}
cout<<"S1中S2出现的次数为:"<<i<<endl;
}
/* 字符串数字显示函数 */
void Translate(char *s)
{
cout<<"\n数字英文显示:";
while(*s!='\0')
{ if(*s<='9'&&*s>='0') //判断是否是数字字符,是则输出
switch(*s-'0')
{
case 0: cout<<"zero-";break;
case 1: cout<<"one-";break;
case 2:cout<<"two-";break;
case 3:cout<<"three-";break;
case 4:cout<<"four-";break;
case 5:cout<<"five-";break;
case 6:cout<<"six-";break;
case 7:cout<<"seven-";break;
case 8:cout<<"eight-";break;
case 9:cout<<"nine-";break;
}
s++;
}
cout<<"\b \b";
}
/* 字符串信息获取函数 */
void GetInfo(char *s)
{
int i,j,k; //分别用于计数
i=j=k=0;
while(*s!='\0')
{
if(*s<='Z'&&*s>='A') i++;
if(*s<='z'&&*s>='a') j++;
if(*s<='9'&&*s>='0') k++;
s++;
}
cout<<"该字符中大写字母个数:"<<i<<endl;
cout<<" 小写字母个数:"<<j<<" 数字个数:"<<k;
}
/* 入口函数 */
void main()
{
int k;
char S1[N],S2[N];
cout<<endl<<"请输入S1:";
cin>>S1;
GetInfo(S1);
Translate(S1);
cout<<"\n请输入S2:";
cin>>S2;
Comparison(S1,S2);
cin>>k;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-22
stl就是懒。
第2个回答  2010-05-21
#include <string>
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

class QUESSION2:public std::unary_function<char,void>
{
public:
QUESSION2(int& A, int& B, int& C)
:A(A),B(B),C(C)
{
this->A = A;
this->B = B;
this->C = C;
}
void operator()(char c)
{
if (c >= 'A' && c <= 'Z')
{
A++;
}
else if (c >= 'a' && c <= 'z')
{
B++;
}
else if (c >= '0' && c <= '9')
{
C++;
}
}
private:
int& A;
int& B;
int& C;
};

char* pStr[]=
{
"ZERO-"
"ONE-",
"TWO-",
"THREE-",
"FOUR-",
"FIVE-",
"SIX-",
"SEVEN-",
"EIGHT-",
"NIGHT-",
"TEN-",
};
class QUESSION3:public std::unary_function<char,void>
{
public:
void operator()(char c)
{
switch (c)
{
case '0':
cout<<pStr[0];
break;
case '1':
cout<<pStr[1];
break;
case '2':
cout<<pStr[2];
break;
case '3':
cout<<pStr[3];
break;
case '4':
cout<<pStr[4];
break;
case '5':
cout<<pStr[5];
break;
case '6':
cout<<pStr[6];
break;
case '7':
cout<<pStr[7];
break;
case '8':
cout<<pStr[8];
break;
case '9':
cout<<pStr[9];
break;
default:
cout<<c;
break;
}
}
};

int count_str(string total_str, string mask_str)
{
int nNum = 0;
int nPos = 0;
do
{
nPos = total_str.find(mask_str, nPos);
if(nPos != -1)
{
nNum++;
nPos += mask_str.size();
}
} while (nPos != -1);

return nNum;
}

int _tmain(int argc, _TCHAR* argv[])
{
string total_str,mask_str;
total_str="123asdfSDFASDFasdf4561237aDFSDFASDFsdfasdf12389123";
mask_str="123";
//(1)请编写在字符串S1查找字符串S2出现次数的函数;
cout<<"QUESSION1:"<<count_str(total_str, mask_str)<<endl;
//(2)输入一串字符,统计各字符出现的次数(分为大写字母、小写字母、数字);
int A = 0, B = 0, C = 0;
for_each(total_str.begin(), total_str.end(), QUESSION2(A,B,C));
cout<<"QUESSION2"<<"大写字母"<<A<<" 小写字母"<<B<<" 数字"<<C<<endl;
//将数字编号“翻译”成英文编号。例如:将编号35706“翻译”成英文编号为:three-five-seven-zero-six
string test_str="0983343434";
cout<<"QUESSION3:"<<endl;
for_each(test_str.begin(), test_str.end(), QUESSION3());
cout<<endl;

return 0;
}

今天就管点闲事吧....
我是STL流....不知楼主是何派本回答被提问者采纳
第3个回答  2010-05-21
呵呵 其实是来看看 不过也会做
但还是不想做。
相似回答