11

C语言编程>第八周 ① 请编写一个函数void fun(char*t,int p[]),统计在t字符串中“a...

 3 years ago
source link: https://blog.csdn.net/qq_45385706/article/details/111325732
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C语言编程>第八周 ① 请编写一个函数void fun(char*t,int p[]),统计在t字符串中“a”到“z”26个字母各自出现的次数,并依次放在p所指数组中。

original.png
朽木自雕i 2020-12-29 00:39:14 articleReadEyes.png 1124
分类专栏: C语言程序设计

例题:请编写一个函数void fun(char*t,int p[]),统计在t字符串中“a”到“z”26个字母各自出现的次数,并依次放在p所指数组中。

例如,当输入字符串sdfssdrefggrthdg后,程序的输出结果应该是: 00031231000000000231000000
请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

代码如下:

#include<conio.h>
#include<stdio.h>
void fun(char*t,int p[])
{
	int j;
	for(j=0;j<26;j++)
	p[j]=0;
	while(*t)
	{
		switch(*t)
		{
		case 'a':p[0]++; break;
		case 'b':p[1]++; break;
		case 'c':p[2]++; break;
		case 'd':p[3]++; break;
		case 'e':p[4]++; break;
		case 'f':p[5]++; break;
		case 'g':p[6]++; break;
		case 'h':p[7]++; break;
		case 'i':p[8]++; break;
		case 'j':p[9]++; break;
		case 'k':p[10]++;break;
		case 'l':p[11]++;break;
		case 'm':p[12]++;break;
		case 'n':p[13]++;break;
		case 'o':p[14]++;break;
		case 'p':p[15]++;break;
		case 'q':p[16]++;break;
		case 'r':p[17]++;break;
		case 's':p[18]++;break;
		case 't':p[19]++;break;
		case 'u':p[20]++;break;
		case 'v':p[21]++;break;
		case 'w':p[22]++;break;
		case 'x':p[23]++;break;
		case 'y':p[24]++;break;
		case 'z':p[25]++;break;
		}
		t++;
	}
}
main()
{
	char a[2000];
	int b[26],i;
	FILE*out;
	printf("\nPlease input a char string:");
	scanf("%s",a);
	fun(a,b);
	for(i=0;i<26;i++)
	printf("%d",b[i]);
	printf("\n");
	fun("a bosom friend after brings a distant land near",b);
	out=fopen("outfile.dat","w");
	for(i=0;i<26;i++)
	fprintf(out,"%d\n",b[i]);
	fclose(out);
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK