-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetMostFreq.c
55 lines (41 loc) · 1.1 KB
/
getMostFreq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "arrayUtil.h"
int main (int argc, char *argv[])
{
char *fileName="myFile.txt";
if(argc!=2){
printf("Please enter only one argument\n");
exit(-1);
}
fileName=argv[1];
FILE *fp;
fp = fopen(fileName, "r");
if(fp == NULL){
printf("Error opening file\n");
exit(-1);
}
int count[26] = {0};
char buff[10];
int i=0;
while(fgets(buff, 10, (FILE*)fp)){
for (i = 0; i < strlen(buff); i++) {
char c = buff[i];
//printf("%c",c);
if(c <= 'z'&& c >= 'a')
count[c-'a']++;
else if(c <= 'Z'&& c >= 'A')
count[c-'A']++;
}
}
fclose(fp);
int max=0, maxIndex=0;
for (i = 0; i < 26; i++) {
if(max < count[i]){
max = count[i];
maxIndex = i;
}
}
printf("The most frequent letter is '%c' , it appears %d times \n", 'a'+maxIndex, max);
}