-
Notifications
You must be signed in to change notification settings - Fork 1
/
frequency_counting.c
60 lines (58 loc) · 1.51 KB
/
frequency_counting.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
56
57
58
59
60
#include <stdio.h>
int main()
{
FILE *fP;
unsigned int arr[60] = {0};
fP = fopen("abc.txt", "r");
char ch;
char alpha[60]={'A','B','C','D','E','F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O', 'P' ,'Q' ,'R' ,'S', 'T','U','V','W','X','Y','Z','.',' ',',','$','!','&','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
while (1)
{
ch = fgetc(fP);
if (ch == EOF)
{
break;
}
else if (('A' <= ch <= 'Z') || ('a' <= ch <= 'z'))
{
// printf("%d",ch);
arr[ch - 'A']++;
}
else
{
switch (ch)
{
case '.':
arr[26]++;
break;
case ' ':
arr[27]++;
break;
case ',':
arr[28]++;
break;
case '$':
arr[29]++;
break;
case '!':
arr[30]++;
break;
case '&':
arr[31]++;
break;
default:
printf("\nNot valid character");
break;
}
}
}
printf("Character | Frequency\n");
printf("***************************\n");
for (int i = 0; i <= 59; i++)
{
printf("%c %14d\n",alpha[i],arr[i]);
}
printf("\n");
fclose(fP);
return 0;
}