ADT
aaron-thesis
arm-eh
ast-experimental
cleanup-dtors
deferred_resn
demangler
enum
forall-pointer-decay
jacob/cs343-translation
jenkins-sandbox
new-ast
new-ast-unique-expr
new-env
no_list
persistent-indexer
pthread-emulation
qualifiedEnum
resolv-new
with_gc
|
Last change
on this file since 92c0f81 was 9c14ae9, checked in by Rob Schluntz <rschlunt@…>, 9 years ago |
|
add thesis source
|
-
Property mode
set to
100644
|
|
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.