source: doc/rob_thesis/examples/tuples/mrv_3.c @ 9fcdfa3

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9fcdfa3 was 9c14ae9, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

add thesis source

  • Property mode set to 100644
File size: 739 bytes
RevLine 
[9c14ae9]1#include <stdio.h>
2#include <ctype.h>
3
4[int, char] most_frequent(const char * str) {
5  char freqs [26] = { 0 };
6  int ret_freq = 0;
7  char ret_ch = 'a';
8  for (int i = 0; str[i] != '\0'; ++i) {
9    if (isalpha(str[i])) {        // only count letters
10      int ch = tolower(str[i]);   // convert to lower case
11      int idx = ch-'a';
12      if (++freqs[idx] > ret_freq) {  // update on new max
13        ret_freq = freqs[idx];
14        ret_ch = ch;
15      }
16    }
17  }
18  return [ret_freq, ret_ch];
19}
20
21void dothing(const char * str) {
22  int freq;
23  char ch;
24  [freq, ch] = most_frequent(str);
25  printf("%s -- %d %c\n", str, ret_freq, ret_ch);
26}
27
28int main() {
29  dothing("hello");
30  dothing("hello, world!");
31  dothing("aaabbbba");
32  dothing("");
33}
Note: See TracBrowser for help on using the repository browser.