-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter-freq-in-str.c
46 lines (40 loc) · 1019 Bytes
/
letter-freq-in-str.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
/*
Problem: Letter frequency in a string
Description
Write a program that gets an string from the user (ends with ‘\n’) and displays the number of times each letter appears in it.
input:
- Line 1 contains a string , for example "Hello, world!"
output: the number of times each letter appear in the format <LETTER> <TIMES>, each letter a line, for example
d 1
e 1
h 1
l 3
o 2
r 1
w 1
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 1000
int main()
{
char str[MAX_STRING_LENGTH];
int freq[26] = { 0 }; // Frequency array for each letter
// Read input
fgets(str, MAX_STRING_LENGTH, stdin);
// Count frequency of each letter
for (int i = 0; i < strlen(str); i++) {
char ch = tolower(str[i]);
if (ch >= 'a' && ch <= 'z') {
freq[ch - 'a']++;
}
}
// Display results
for (int i = 0; i < 26; i++) {
if (freq[i] > 0) {
printf("%c %d\n", i + 'a', freq[i]);
}
}
return 0;
}