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

来源:互联网 发布:淘宝网优依裤 编辑:程序博客网 时间:2024/06/10 08:26

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

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

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

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

 

#include <stdio.h>#define Maxsize 52typedef struct{ char data; int freq;//save the frequentness}ElemType;typedef struct{ ElemType Word[Maxsize];}Node;//Creat a word list that include a~z,A~ZNode 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();}



 

1 1
原创粉丝点击