Count the number of occurrences of the English letter az in a piece of text (via array)

Topic: Realize a count of the number of occurrences of English letters az in a paragraph of text.

The user enters a piece of English, the program counts the number of occurrences of the English letter az (uppercase as lowercase), and lists the most frequent letters and the number of times.

Requirements: The counting code segment must not use 20 or more if statements.

Problem-solving ideas: A piece of English characters entered by the user may contain lowercase letters, uppercase letters and other characters, so you need to first convert the possible uppercase letters into corresponding lowercase letters; then count the number of each letter, if you use it directly If statement and switch, the amount of program will be very large, so consider using array.

Look at the main function first

int main()
 {
    
    
 	char str1[500];    //用户输出的字符串长度不大于500
 	printf("请输入一段英文字符串:\n");
    gets(str1);        //用户输入
 	xiaoxie(str1);     //把可能的大写转化为小写
 	tongji(str1);      //程序主要部分:统计
 	return 0;
 } 

In the main function, we need to define two more functions: xiaoxie() and tongji(), as follows:

Function one: xiaoxie()

/************************
函数:void xiaoxie(char *str0)
功能:把字符串中的大写字母转化为小写字母
参数:字符串 char *str0
返回值:无
*************************/
void xiaoxie(char *str0)
{
    
    
    int count = strlen(str0);    //获得字符串str0的长度
 	for(int i=0;i<count;i++)
 	{
    
    
	 	if(str0[i]>='A' && str0[i]<='Z')
	 		str0[i] = str0[i] + 32; 	//大写字母的ASCII码加32即为对应的小写字母
	 }
}

Function 2: The tongji() statistical function is the focus of this program, and its interior can be divided into several parts, as follows:

1) Count the number of occurrences of each letter

Two arrays are defined here to store the letter az and the number of times it appears. Among them, zimu[] is a string array, and cishu[] is an ordinary array. Two for loops are nested to achieve statistics on the number of letters appearing. First A for loop is from the first character entered by the user to the last character in turn, and the second for loop is to sequentially determine which letter the character belongs to in az
//统计各字母出现的个数 
char zimu[26]="abcdefghijklmnopqrstuvwxyz";
int cishu[26]={
    
    0};
int count = strlen(str0);
for(int i=0;i<count;i++)
{
    
    
    for(int j=0;j<26;j++)
    {
    
    
        if(str0[i]==zimu[j])
         	cishu[j]++;
 	}	
}

2) Print out the number of each letter on the screen

printf("统计各字母出现的个数:\n");
for(int i=0;i<26;i++)
{
    
    
	printf("%c:%d\t",zimu[i],cishu[i]);
	if((i+1)%6==0)
		printf("\n");    //每6个字母就换行
}

3) Count the most frequently occurring letters and their frequency

This is the focus of this tongji() function, and I think it is also difficult. If you simply count a letter that appears most frequently, the program is very simple, as follows:
//统计出现频率最高的字母 
int t,max=cishu[0];
for(int i=0;i<26;i++)
{
    
    
	if(max<cishu[i])
	{
    
    
		max = cishu[i];
		t = i;
	}
}
printf("\n出现次数最多的字母是:%c,共出现%d次\n",zimu[t],max);

But when there are multiple letters with the highest frequency, it can only output the first one.
Insert picture description here
This is obviously not enough, so it needs to be improved, as follows:

int t,max=cishu[0];
int j=0,c=0,a[1]={
    
    0};
for(int i=0;i<26;i++)
{
    
    
	if(max<=cishu[i])
	{
    
    
		if(max==cishu[i])
		{
    
    
			a[j]=i;   //出现字母次数相同时记下对应的i(存放到数组)
            j++;      //j为数组a[]组索引
        }
	    else   //即max<cishu[i]
		{
    
    
			max = cishu[i];
			t = i;    //更新max对应的i
			c = j;	  //当旧的(数组存放的)字母次数不等于新的字母次数时,记下对应的j
		}
	}
}
printf("\n出现次数最多的字母是:%c",zimu[t]);
for(int i=c;i<j;i++)
{
    
    
	printf("、%c",zimu[a[i]]);
}
printf(",共出现%d次\n",max);

The biggest difference between the improved and before is that a letter with the same number of occurrences is added, that is

if(max==cishu[i])
{
    
    
   a[j]=i;
   j++;
}

The complete tongji() function is as follows:

/************************
函数:void tongji(char *str0)
功能:统计字母a-z出现的次数,并得到出现频率最高的字母及次数
参数:字符串 char *str0
返回值:无
*************************/
void tongji(char *str0)
{
    
    
    char zimu[26]="abcdefghijklmnopqrstuvwxyz";
    int cishu[26]={
    
    0};
    int count = strlen(str0);
 	for(int i=0;i<count;i++)
 	{
    
    
    	 	for(int j=0;j<26;j++)
    		{
    
    
 			if(str0[i]==zimu[j])
 				cishu[j]++;
 		}	
	}
	printf("统计各字母出现的个数:\n");
	for(int i=0;i<26;i++)
	{
    
    
		printf("%c:%d\t",zimu[i],cishu[i]);
		if((i+1)%6==0)
			printf("\n");
	}
	int t,max=cishu[0];
	int j=0,c=0,a[1]={
    
    0};
	for(int i=0;i<26;i++)
	{
    
    
		if(max<=cishu[i])
		{
    
    
			if(max==cishu[i])
			{
    
    
				a[j]=i;
				j++;
			}
			else
			{
    
    
				max = cishu[i];
				t = i;
				c = j;
			}
		}
	}
	printf("\n出现次数最多的字母是:%c",zimu[t]);
	for(int i=c;i<j;i++)
	{
    
    
		printf("、%c",zimu[a[i]]);
	}
	printf(",共出现%d次\n",max);
}

Complete code

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

void xiaoxie(char *str0)
{
    
    
	int count = strlen(str0);
 	for(int i=0;i<count;i++)
 	{
    
    
	 	if(str0[i]>='A' && str0[i]<='Z')
	 		str0[i] = str0[i] + 32; 	
	 }
}

void tongji(char *str0)
{
    
    
	char zimu[26]="abcdefghijklmnopqrstuvwxyz";
	int cishu[26]={
    
    0};
	int count = strlen(str0);
 	for(int i=0;i<count;i++)
 	{
    
    
	 	for(int j=0;j<26;j++)
		{
    
    
 			if(str0[i]==zimu[j])
 				cishu[j]++;
 		}	
	}
	printf("统计各字母出现的个数:\n");
	for(int i=0;i<26;i++)
	{
    
    
		printf("%c:%d\t",zimu[i],cishu[i]);
		if((i+1)%6==0)
			printf("\n");
	}
	int t,max=cishu[0];
	int j=0,c=0,a[1]={
    
    0};
	for(int i=0;i<26;i++)
	{
    
    
		if(max<=cishu[i])
		{
    
    
			if(max==cishu[i])
			{
    
    
				a[j]=i;
				j++;
			}
			else
			{
    
    
				max = cishu[i];
				t = i;
				c = j;	
			}
		}
	}
	printf("\n出现次数最多的字母是:%c",zimu[t]);
	for(int i=c;i<j;i++)
	{
    
    
		printf("、%c",zimu[a[i]]);
	}
	printf(",共出现%d次\n",max);
}

 int main()
 {
    
    
 	char str1[500];
 	printf("请输入一段英文字符串:\n");
	gets(str1);
 	xiaoxie(str1);
 	tongji(str1);
 	return 0;
 }

The operation effect is as follows:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46977029/article/details/109281888