Last change
on this file since a0bd9a2 was
67982887,
checked in by Peter A. Buhr <pabuhr@…>, 6 years ago
|
specialize thesis directory-names
|
-
Property mode set to
100644
|
File size:
699 bytes
|
Line | |
---|
1 | #include <stdio.h> |
---|
2 | #include <ctype.h> |
---|
3 | |
---|
4 | int most_frequent(const char * str, char * ret_ch) { |
---|
5 | char freqs [26] = { 0 }; |
---|
6 | int ret_freq = 0; |
---|
7 | for (int i = 0; str[i] != '\0'; ++i) { |
---|
8 | if (isalpha(str[i])) { // only count letters |
---|
9 | int ch = tolower(str[i]); // convert to lower case |
---|
10 | int idx = ch-'a'; |
---|
11 | if (++freqs[idx] > ret_freq) { // update on new max |
---|
12 | ret_freq = freqs[idx]; |
---|
13 | *ret_ch = ch; |
---|
14 | } |
---|
15 | } |
---|
16 | } |
---|
17 | return ret_freq; |
---|
18 | } |
---|
19 | |
---|
20 | void dothing(const char * str) { |
---|
21 | char ch; |
---|
22 | int freq = most_frequent(str, &ch); |
---|
23 | printf("%s -- %d %c\n", str, freq, ch); |
---|
24 | } |
---|
25 | |
---|
26 | int main() { |
---|
27 | dothing("hello"); |
---|
28 | dothing("hello, world!"); |
---|
29 | dothing("aaabbbba"); |
---|
30 | dothing(""); |
---|
31 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.