source: doc/theses/rob_schluntz_MMath/examples/tuples/mrv_3.c @ a0bd9a2

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: 739 bytes
Line 
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.