-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreq_table_function.c
More file actions
35 lines (31 loc) · 826 Bytes
/
Copy pathfreq_table_function.c
File metadata and controls
35 lines (31 loc) · 826 Bytes
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "A3header.h"
/*
* Program name: freq_table_function.c
* Author: Rehan Nagoor Mohideen
* Student ID: 1100592
* Purpose: Function to create the frequency table from a string.
*/
int * frequency_table(char *string){
int i, j;
int *letterFreq = malloc(sizeof(int) * 26);
if (letterFreq == NULL) { /*For error handling*/
fprintf(stderr, "error: not enough space for malloc\n");
exit(1);
}
/*To initialize all elements to 0.*/
for (i = 0; i < 26; i++) {
letterFreq[i] = 0;
}
/*To find the number of times each alphabet appears.*/
for (i = 0; i < 26; i++) {
for (j = 0; j < strlen(string); j++) {
if (string[j] == 'a' + i || string[j] == 'A'+ i) {
letterFreq[i]++;
}
}
}
return letterFreq;
}