source:
doc/theses/rob_schluntz_MMath/examples/tuples/mrv_1.c@
8a3d5e7
| Last change on this file since 8a3d5e7 was 67982887, checked in by , 8 years ago | |
|---|---|
|
|
| File size: 745 bytes | |
| Line | |
|---|---|
| 1 | #include <stdio.h> |
| 2 | #include <ctype.h> |
| 3 | struct mf_ret { |
| 4 | int freq; |
| 5 | char ch; |
| 6 | }; |
| 7 | |
| 8 | struct mf_ret most_frequent(const char * str) { |
| 9 | char freqs [26] = { 0 }; |
| 10 | struct mf_ret ret = { 0, 'a' }; |
| 11 | for (int i = 0; str[i] != '\0'; ++i) { |
| 12 | if (isalpha(str[i])) { // only count letters |
| 13 | int ch = tolower(str[i]); // convert to lower case |
| 14 | int idx = ch-'a'; |
| 15 | if (++freqs[idx] > ret.freq) { // update on new max |
| 16 | ret.freq = freqs[idx]; |
| 17 | ret.ch = ch; |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | return ret; |
| 22 | } |
| 23 | |
| 24 | void dothing(const char * str) { |
| 25 | struct mf_ret ret = most_frequent(str); |
| 26 | printf("%s -- %d %c\n", str, ret.freq, ret.ch); |
| 27 | } |
| 28 | |
| 29 | int main() { |
| 30 | dothing("hello"); |
| 31 | dothing("hello, world!"); |
| 32 | dothing("aaabbbba"); |
| 33 | dothing(""); |
| 34 | } |
Note:
See TracBrowser
for help on using the repository browser.