source: doc/theses/rob_schluntz/examples/tuples/mrv_1.c @ 6926a6d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 6926a6d was 728df66, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

more documentation name changes

  • Property mode set to 100644
File size: 745 bytes
Line 
1#include <stdio.h>
2#include <ctype.h>
3struct mf_ret {
4  int freq;
5  char ch;
6};
7
8struct 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
24void 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
29int main() {
30  dothing("hello");
31  dothing("hello, world!");
32  dothing("aaabbbba");
33  dothing("");
34}
Note: See TracBrowser for help on using the repository browser.