笨方法实现字符串中字符频度的统计

 

1.先建立一个字母表,包含字母的大小写

2.输入需要统计频度的字符串。

3.用字符串中的字符去遍历字母表,频度存放在字母表的freq变量中。

如果大家有什么好方法,非常欢迎留言!

 

#include <stdio.h>
#define Maxsize 52
typedef struct{
 char data;
 int freq;//save the frequentness
}ElemType;

typedef struct{
 ElemType Word[Maxsize];
}Node;


//Creat a word list that include a~z,A~Z
Node CreateWordList(Node Word26)
{
 int j=0;
 char i;
  //for(i='A';i<='Z';i++,j++);
 i='A';
 while(i<='Z')
 {
  Word26.Word[j].data=i;
  Word26.Word[j].freq=0;
  j++;
  i++;
 }
 //for(i='a';i<='z';i++,j++);
 // Word26.Word[j].data=i;
  i='a';
 while(i<='z')
 {
  Word26.Word[j].data=i;
  Word26.Word[j].freq=0;
  j++;
  i++;
 }
 return Word26;
}

void main()
{
 int i=0,j=0;
 Node WordNode;
 Node Word26;
 char Input[10];
 Word26=CreateWordList(Word26);
 printf("input 10 word;\n");
 scanf("%s",Input);//input the string that you want to calculate the frequentness

 for(j=0;j<52;j++)//j record the Word26's Word position

 for(i=0;i<10;i++)//i record the input string array position
  if(Input[i]==Word26.Word[j].data)
   Word26.Word[j].freq++;

 
    for(j=0;j<52;j++)
  if(Word26.Word[j].freq)//print fliter
  printf(" %c: %d\n ",Word26.Word[j].data,Word26.Word[j].freq);//print the input word's frequentness
 
 getch();

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值