一个字符串,大写字母算作小写字母来统计字母出现次数,

来源:百度知道 编辑:UC知道 时间:2024/05/16 07:55:44
假如字符串是bbCca*,则输出b:2 C:2 a:1 *:1,按字母首次出现的顺序输出。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void calc(string, string::size_type);
int _tmain(int argc, _TCHAR* argv[])
{
int hxy;
calc("bbuuake", 7);
cin>>hxy;
return 0;
}
void calc( std::string input, string::size_type len)
{
int count=0;
string out="";
out=input;
for(string::size_type i=0;i<len;i++)
{
for(string::size_type j=0;j<len;j++)
{
if(out[i]==input[j])
++count;
}
cout<<out[i]+":"+count<<endl;
count=0;
}
}
此代码有什么问题,输出结果不对

#include <iostream>
#include <string>
using namespace std;
void calc(char *op, int);
int main() {
char s1[8]="bbuuake";
calc(s1, 7);
return 0;
}
void calc(char *input, int len) {
int count=0;
char *op1= new char[8];
op1=input;
for (int i=0; i<len; i++) {
for(int k=0;k<i;k++){
if(op1[k]==op1[i])
i++;
}
for(int j=0;j<len;j++){
if(op1[i]==input[j])
count++;
}
cout<<op1[i]<<":"<<count<<endl;
count=0;
}
delete op1;
}
这下行了,在执行输出时判断下就行了。

你没有排除重复的字母,两个b或者两个c的都输出了相同的个数
你应该还定义一个数组,用来记录统计过的字母,然后下一个字母如果不在记录当中再统计它的出现次数

输入一个字符串,将字符串中所有的大写字母变为小写,输出转变后的字符串 输入一个字符串,将其中的小写字母变为大写字母 输入一个字符串,将其中的大写字母改为小写字母,小写字母改为大写字母,然后输出 编写一个函数,求字符串中大写字母的个数。例如:字符串“C++Programing”中,大写字母数=2。 一个函数,该函数返回给定字符串中大写字母字符的个数 一个汇编程序 关于把字符串中的小写字母转换为大写字母 用存储过程/SQL语言,实现 输入一个英语字符串,统计其中大写字母???? 编写程序,一个字符串S以"$"字符结束,统计大写字母A出现的次数. 请问输入一个字符串,然后计算它的大写字母,小写字母,数字,空格和其他的字符有多少个? 化学里,元素一个大写字母加一个小数字表示什么?H加个小5