tyrion

occorrenze.c

Dec 10th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 26
  3.  
  4. int indexof(char c) {
  5.     return c - 'a';
  6. }
  7.  
  8. char valueof(int i) {
  9.     return 'a' + i;
  10. }
  11.  
  12. char occorrenza(char stringa[]) {
  13.     int occorrenze[N] = { 0 };
  14.     int max = 0, i;
  15.     char *c;
  16.  
  17.     for (c = stringa; *c != '\0'; c++) {
  18.          i = indexof(*c);
  19.          if (++occorrenze[i] > occorrenze[max]) max = i;
  20.  
  21.     }
  22.     return valueof(max);
  23. }
  24.  
  25. void main() {
  26.     char test[] = "brrrchefeddddo";
  27.     putchar(occorrenza(test));
  28.     putchar('\n');
  29. }
Advertisement
Add Comment
Please, Sign In to add comment