當前位置:首頁 » 編程語言 » 小程序統計字母頻率c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

小程序統計字母頻率c語言

發布時間: 2022-05-10 06:20:41

1. c語言 求在輸入的字元序列中統計各個小寫字母出現的頻率。

#include "stdio.h"
main()
{ int n[26]={0},i,j;
char m[200];
gets(m);
for(i=0;m[i]!='\0';i++)
if(m[i]>=97 && m[i]<=122)
n[m[i]-97]++; /*取得每個小寫字母出現的個數*/
for(j=0;j<26;j++)
if(n[j]!=0)
printf("%c is %d/%d\n",97+j,n[j],i);
}
或者用下面的程序,更好懂些。

#include "stdio.h"
main()
{ int n[26]={0},i,j;
char m[200];
gets(m);
for(i=0;m[i]!='\0';i++)
if(m[i]>=97 && m[i]<=122)
{switch(m[i])
{
case 'a': n[0]+=1;break;
case 'b': n[1]+=1;break;
case 'c': n[2]+=1;break;
case 'd': n[3]+=1;break;
case 'e': n[4]+=1;break;
case 'f': n[5]+=1;break;
case 'g': n[6]+=1;break;
case 'h': n[7]+=1;break;
case 'i': n[8]+=1;break;
case 'j': n[9]+=1;break;
case 'k': n[10]+=1;break;
case 'l': n[11]+=1;break;
case 'm': n[12]+=1;break;
case 'n': n[13]+=1;break;
case 'o': n[14]+=1;break;
case 'p': n[15]+=1;break;
case 'q': n[16]+=1;break;
case 'r': n[17]+=1;break;
case 's': n[18]+=1;break;
case 't': n[19]+=1;break;
case 'u': n[20]+=1;break;
case 'v': n[21]+=1;break;
case 'w': n[22]+=1;break;
case 'x': n[23]+=1;break;
case 'y': n[24]+=1;break;
case 'z': n[25]+=1;break;
}
}
for(j=0;j<26;j++)
if(n[j]!=0)
printf("%c is %d / %d\n", 97+j, n[j], i);
}

2. 編寫C語言程序統計某給定ASCII文件中各字母的出現頻率

#include<stdio.h>
void main() { FILE *fp; int a[26],i,s; char fnm[80]={ "text.txt" },buffer[256],*p;
s=0; for ( i=0;i<26;i++ ) a[i]=0;
if ( fp=fopen(fnm,"r") ) {
while ( !feof(fp) ) {
fgets(fp,255,buffer); p=buffer;
while ( *p ) {
if ( *p>='a' && *p<='z' ) a[*p-'a']++;
else if ( *p>='A' && *p<='Z' ) a[*p-'A']++;
s++;
}
}

fclose(fp);
for ( i=0;i<26;i++ ) printf("[%c] %.2f%%\n",(float)a[i]*100/s); //顯示百分比(總字元數)
} else printf("無法打開文件讀取。\n");
}

3. c語言小程序,求幫忙寫一個記錄大寫字母、小寫字母、偶數、奇數的出現次數的小程序

#include"stdio.h"
intmain(void){
intU=0,L=0,O=0,E=0,m=0,n,ch='!';
printf("輸入循環次數:");
if(scanf("%d",&n)&&n>0){
printf("將循環%d次 ",n);
while(fflush(stdin),n--&&ch!='/'){
printf("輸入字元:");
if((ch=getchar())>='A'&&ch<='Z'){
printf("大寫字母 ");
U++;
}
elseif(ch>='a'&&ch<='z'){
printf("小寫字母 ");
L++;
}
elseif(ch&1){
printf("奇數 ");
O++;
}
elseif((ch&1)==0){
printf("偶數 ");
E++;
}
m++;
}
printf("退出 ");
printf("循環了%d次 ",m-1);
printf("大寫字母出現次數:%d ",U);
printf("小寫字母出現次數:%d ",L);
printf("偶數出現次數:%d ",E);
printf("奇數出現次數:%d ",O);
}
elseprintf("程序立即結束 ");
return0;
}

4. 如何用C語言統計一個文檔中每個字母的個數和出現的頻率

  1. 用文件操作函數打開文件,挨著讀入字元,統計每個字元出現了多少次,最後用每個字元出現的次數除以所有次數,得到出現頻率。

  2. 常式:

    /*統計一個文件里各個字元出現的次數,這里只統計了26個字母,如果還有更多的字元的話,將數組再擴大就行了*/
    #include<stdio.h>
    #include<math.h>
    voidstat(char*file,int*statistic)
    {
    inti=0;
    while(file[i++]!=0)
    statistic[file[i-1]-97]++;
    }
    intmain()
    {
    charfile[100]={0};
    intstatistic[26]={0};
    inti=0,j;
    doubles=0;
    FILE*fp;
    if((fp=fopen("1.txt","r"))==NULL)
    {
    printf("can'topen1.txt");
    return1;
    }
    while(!feof(fp))
    fread(&file[i++],1,1,fp);
    fclose(fp);
    j=i;
    stat(file,statistic);
    for(i=0;i<26;i++)
    printf("%c:%d ",97+i,statistic[i]);
    return0;
    }

5. 用C語言統計英文26個字母在文本中出現的頻率

#include<stdio.h>

int main()

{

FILE *fp;

int i,a[26]={0},b[26]={0}; char c;

fp=fopen("d:\0.txt","r");

if(fp!=NULL)

do

{c=fgetc(fp);

if(c>='a'&&c<='z')b[c-'a']++;

else if(c>='A'&&c<='Z')a[c-'A']++;

}while(!feof(fp));

for(i=0;i<26;i++)

if(a[i]>0)printf("%3c: %3d",'A'+i,a[i]);

printf(" ");

for(i=0;i<26;i++)

if(b[i]>0)printf("%3c: %3d",'a'+i,b[i]);

printf(" ");

fclose(fp);

return 0;

}

d:.txt內容如下:

Starts a new instance of the MS-DOS command interpreter.


COMMAND [[drive:]path] [device] [/E:nnnnn] [/P] [/C string] [/MSG]


[drive:]path Specifies the directory containing COMMAND.COM file.

device Specifies the device to use for command input and output.

/E:nnnnn Sets the initial environment size to nnnnn bytes.

/P Makes the new command interpreter permanent (can't exit).

/C string Carries out the command specified by string, and then stops.

/MSG Specifies that all error messages be stored in memory. You

need to specify /P with this switch.

6. 統計字母的使用頻率

分怎麼少要求這么多,我也簡單點回答: 寫出了代碼還是要給你解釋 ..
分析:1.定義:str[200]裝字元串,s[26]裝26個字母對應出現的頻率,

根據你的要求:
(根據三的分析,主要問題在於:)內容
*p=str n=0; 由於只有英文字母數組中,所以:
if('a'<=*p<='z')while(*p){s[*p-'a']++;p++} /*對於s[*p-'a']++;解釋一下:if里說明了是 小寫字母,所以*p-'a'就是對應的acs2碼相減, 得出的數也就是字母a後面的數..*/
if()就把上面小寫改成大寫就行 //這里A與a都統計在s[0]里 ..類推..

這樣出現的次數就統計在s[26]里了 排序會撒?常用冒泡法 ..
創新要求: 就是文件那章內容 讀寫而已 字元串不是靠手輸入str[]中...fgets

7. 字母頻率統計-C語言

1、char a2[][26]={0};

按照你這種寫法 這個數組只有一行,也就是等效於char a2[1][26]={0};

所以當出現超過一個字元的時候 調用越界,導致崩潰。

如果你准備用二維數組的話,那麼大小需要動態分配。

但實際上,沒必要用數組,直接輸出就好了。

2、 輸入沒有說以.結束,而是說以ctrl+z結束。

改了一版代碼,你看看:

#include<stdio.h>
intmain()
{
intc;
inta[26]={0};
inti=0,j=0,max;

while((c=getchar())!=EOF)
{
if(c>='a'&&c<='z')
a[c-'a']++;
}//用a[0]大小表示字母a出現次數,其他字母分別用a[1],a[2]…a[25]表示。

for(i=1,max=a[0];i<26;i++)
{
if(a[i]>max)
max=a[i];
}
for(i=max;i>0;i--)
{
for(j=0;j<26;j++)
{
if(a[j]>=i)putchar('*');
elseputchar('');
}
putchar(' ');
}
printf("abcdefghijklmnopqrstuvwxyz ");
return0;
}

8. C語言如何統計並輸出不同字元在文章中出現的次數或頻率(空格、換行、標點等也按字元處理)

至於思路嘛,其實也沒什麼,就是分別進行判斷是否數字、字母、空格之類,並和用自增運算符來統計數量。具體看注釋。

#include"stdio.h"
void main()
{
int qita=0,english=0,space=0,number=0,i,j;

while((i=getchar())!='\n') /**執行循環,把獲得的字元賦給i,直到i等於換行符\n時循環終止**/
{
if(('a'<=i&&i<='z')||('A'<=i&&i<='Z'))
english++; /**如果a<=i<=z或A<=i<=Z,則字母的個數自加一**/
else if('0'<=i&&i<='9') /**如果是0--9之間的數字,則執行下面的number++**/
number++;
else if(i==' ') /**這個是判斷空格的**/
space++;
else
qita++; /**這個是統計其他未知字元個數**/
}
printf("english=%d,space=%d,number=%d,qita=%d",english,space,number,qita);
getch();
}

9. 設字元串是由26個大寫字母構成,試編程統計每個字母出現的頻率

首先做一個計數器
count=0;
然後循環遍歷字元串,如果
str[i]>='a'
&&
str[i]<='z'
,那麼count++;
最後count就是大寫字母的個數