新手问题.求助.谢谢

zqhyeah 2006-07-28 06:50:24
计算字符串中每个字母出现的次数
请用C语言编写一个程序。首先,接收用户通过键盘输入的一串字符串,该字符串应该全部是由26个英文字母组成。然后,通过对该字符串进行比较、统计处理,统计出该字符串中每个字母出现的次数。最后,仅将该字符串中出现过的字母以及该字母在字符串中所出现的次数输出到屏幕上。具体要求如下:
1、 在主程序中,实现对字符串的输入、比较统计和输出功能。其中要求:通过调用函数checkstring()来完成对所输入字符串中字母的逐一比较,统计获取每个字母在字符串中出现的次数,并保存在相应的数组变量中。
2、 在接收字符串时,通过屏幕输出提示信息:“请输入一串英文字母(最多50个字符):”,接收键盘输入的一串字符。
要求:
1)、对于所接收的字符串,应该验证其长度是否超出了你所预先定义的字符串变量的长度(请见下面提示)。如果超出变量的长度,应该给出提示信息:“输入的英文字母数超长了!“。
2)、在显示了提示信息后,运行结束。
3、 对于比较、统计函数checkstring() 要求,对所输入字符串中的每个字符进行比较、统计,获得26个英文字母在此字符串中出现的次数。
要求:
1)、此题涉及到了字符串的操作,需要引用<string.h>头文件
2)、字符串在C语言中应该定义为字符数组。
3)、为了获得输入字符串的长度,需要函数strlen().
int len;
len=strlen(str);
4)、为了对大小写的英文字母进行相同的处理,可以调用C函数库提供的toupper()函数和tolower()函数将输入字符串中的英文字母统一转换成大写或者小写字母,然后再对所输入的字符串中的所有英文字母进行逐个的比较、统计。
为了调用C语言函数库中的toupper()函数或tolower()函数,需要引用<stdlib.h>头文件。
示例:将字符串str 中的英文字母统一转换为小写字母的代码如下:
char str1[51];
int I;
for(I=0;I<len;I++)
str1[I]=tolower(str[I]);
4、 输出时,要求只输出所接收到的字符串中涉及到的所有字母,以及在该字符串中该字母所出现的次数
5、 在实现功能的同时,要求注意编程的规范:程序逻辑分明,关键代码有注释,书写有缩进,判断输入有错误时,应输出提示信息。
6、 请按照以下输入的数据运行,运行结果应该为:

刚学VC不久.不是很会.谢谢指点指点!
...全文
266 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhu_doc 2006-07-29
  • 打赏
  • 举报
回复
#include"stdio.h"
#include"string.h"
main()
{int i,j,b[26];
char a[30];
for(i=0;i<26;i++)
b[i]=0;
gets(a);
printf("the length is %d\n",strlen(a));
for(i=0;i<strlen(a);i++)
{for(j=0;j<26;j++)
if(a[i]=65+j || a[i]=97+j)
b[j]=b[j]+1;}
for(i=0;i<26;i++)
printf("%c: %d ",65+i,b[i]);
}
cunsh 2006-07-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

typedef map<char , int> Map_char_int;

template<class T1, class T2>
void print_pair(const pair<T1,T2>& x)
{
cout << x.first << "\t" << x.second << endl;
}

int main()
{
cout << "Input: " << endl;
string s;
getline(cin, s);

Map_char_int mci;
for (string::iterator iter = s.begin(); iter != s.end(); ++iter)
++mci[*iter];

for_each(mci.begin(), mci.end(),
(void(*)(const pair<char,int>&))print_pair<char,int> );

return 0;
}

zqhyeah 2006-07-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void checkstring(char *ch , int *k );

void main()
{
char str[50] ;//定义一个字符数组
int i , k[26] , len;
printf("请输入一串英文字母(最多50个字符)\n");
fflush(stdin);
gets(str); //接受字符串
len=strlen(str); //计算字符串长度
for(i=0;i<26;i++)
k[i]=0;
for(i=0;i<len;i++)
str[i]=tolower(str[i]); //转换成小写字母
if(len>50)
{
printf("输入的英文字母数超长了\n");
exit(0);
}

checkstring(str,k); //调用函数
printf("在字符串%s中每个英文字母出现的次数是:\n",str);
for(i=0;i<26;i++)
if(k[i]>0)
{
printf("%c %d\n",('a'+i),k[i]); //输出每个字符的出现次数
}


}
void checkstring(char *ch , int *k)
{
int m=0 ;

for (m=0;ch[m]!='\0' ;m++ ) //统计字母出现的次数
k[(ch[m])-'a']++;
}
这个是我自己做的.抄了一下.不过[(ch[m])-'a']不是很明白~
flying751 2006-07-29
  • 打赏
  • 举报
回复
楼上的代码确实很精美!
张大山No100 2006-07-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

#define STRING_LENGTH 512

char str[STRING_LENGTH];

int main(int argc, char* args[])
{
int i,j;
int num[26]={0};
printf("input the string:");
scanf("%s",str);

for (i=0;str[i]!='\0' ;++i ) num[toupper(str[i])-'A']++;

for (i=0;i<26 ;++i ) printf("%c : %d\n",'A'+i,num[i]);

return 0;
}

我喜欢这段代码,短而又精简,很优美!
jixingzhong 2006-07-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

根据你的要求写的,
楼主你看看吧 ....

如果有不符合的,
再自己改改 ...
jixingzhong 2006-07-29
  • 打赏
  • 举报
回复
#define MLEN 50

void checkstring(char *src, int countT[])
{
int i=0;
while(src[i] != '\0')
countT[ src[i++]-'a' ]++;
}

int main()
{
char str[MLEN]={0};
int i, countT[26]={0};

printf("请输入一串英文字母(最多50个字符):");
gets(str);
for(i=0; i<strlen(str); i++)
str[i] = tolower(str[i]);
printf("Tolower string is %s, the length is %d \n",str, strlen(str));
if(str[49] != '\0')
{
printf("输入的英文字母数超长了!\n");
system("PAUSE");
return -1;
}
checkstring(str, countT);
for(i=0; i<strlen(str); i++)
if(countT[i]!=0) printf("In string %s, count %c or %c is %d\n", str, 'a'+i, 'A'+i, countT[i]);
system("PAUSE");
return 0;
}
tailzhou 2006-07-28
  • 打赏
  • 举报
回复
好多分哟!

#include <stdio.h>
#include <string.h>

#define STRING_LENGTH 512

char str[STRING_LENGTH];

int main(int argc, char* args[])
{
int i,j;
int num[26]={0};
printf("input the string:");
scanf("%s",str);

for (i=0;str[i]!='\0' ;++i ) num[toupper(str[i])-'A']++;

for (i=0;i<26 ;++i ) printf("%c : %d\n",'A'+i,num[i]);

return 0;
}
zqhyeah 2006-07-28
  • 打赏
  • 举报
回复
如何统计各个字符出现的次数,不是很明白...谢谢各位下虾了;
snowbirdfly 2006-07-28
  • 打赏
  • 举报
回复
是不是发错地方了???

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧