当前位置:网站首页>Count the frequency of letters in text (case insensitive)

Count the frequency of letters in text (case insensitive)

2020-11-07 20:19:13 PamShao

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 10240

void count(char* n)
{
	int i = 0, s[26] = { 0 };
	while (n[i] != NULL)
	{
		if (n[i] >= 'A' && n[i] <= 'Z')
		{
			n[i] += 32;
			s[n[i] - 'a']++;
			i++;
		}
		else if (n[i] >= 'a' && n[i] <= 'z')
		{
			s[n[i] - 'a']++;
			i++;
		}
		else
			i++;
	}

	for (i = 0; i < 26; i++)
	{
		printf("%c appear :%d Time \n", (char)(i + 'a'), s[i]);
	}
}
int main() {
	char buf[MAX_NUM];			// Character buffer 
	FILE* fp;
	//fopen("doc.txt", "r+"), Read the content of the text 
	if ((fp = fopen("doc.txt", "r+")) == NULL) {
		perror("the file fail to read");
		exit(1);
	}
	while (!feof(fp) && !ferror(fp)) {	// File read end or error exit 
   //while(fgets(buf,MAX_NUM,fp) != NULL)
		fgets(buf, MAX_NUM, fp);// Read one line at a time or MAX_NUM Characters 
		printf(" The content is :%s \n", buf);
		count(buf);
	}
	fclose(fp);				// Close file 
	return 0;
}

 

版权声明
本文为[PamShao]所创,转载请带上原文链接,感谢