Computer Programming Contest Preparation

ToolBox - Source for: 4/499/a.c



/home/toolbox/public_html/solutions/4/499/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2015-03-11
   19  * Purpose:
   20  * Problem: 499
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define MAX_LINE 257
   28 char line[MAX_LINE];
   29 int cnt[52];
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34     int i;
   35 
   36     for (i=0; i<52; i++)
   37         {
   38             /* for */
   39             cnt[i] = 0;
   40         } /* for */
   41 } /* FUNCTION init */
   42 
   43 void dump()
   44 {
   45     /* FUNCTION dump */
   46 } /* FUNCTION dump */
   47 
   48 int getInput()
   49 {
   50     /* FUNCTION getInput */
   51     int dataReadFlag;
   52 
   53     fgets(line, MAX_LINE, stdin);
   54     if (feof(stdin))
   55         dataReadFlag = FALSE;
   56     else
   57         {
   58             /* something to read */
   59             dataReadFlag = TRUE;
   60             line[strlen(line)-1] = 0;
   61         } /* something to read */
   62     return (dataReadFlag);
   63 } /* FUNCTION getInput */
   64 
   65 void process()
   66 {
   67     /* FUNCTION process */
   68     int i;
   69     int mx = 0;
   70 
   71     init();
   72     /* process the line */
   73     for (i=0; i<strlen(line); i++)
   74         {
   75             /* for */
   76             if (isupper(line[i]))
   77                 {
   78                     /* upper */
   79                     cnt[line[i]-'A']++;
   80                     DEBUG printf("%c %d %d\n", line[i], line[i], line[i]-'A');
   81                 } /* upper */
   82             if (islower(line[i]))
   83                 {
   84                     /* lower */
   85                     cnt[line[i]-'a'+26]++;
   86                 } /* lower */
   87         } /* for */
   88     /* find the max count */
   89     for (i=0; i<52; i++)
   90         {
   91             /* for */
   92             if (cnt[i] > mx)
   93                 {
   94                     mx = cnt[i];
   95                 }
   96         } /* for */
   97     /* print out the upper case matches */
   98     for (i=0; i<26; i++)
   99         {
  100             /* for */
  101             if (mx == cnt[i])
  102                 {
  103                     /* if */
  104                     DEBUG printf("(%d,%d,%c)", i, i+'A', i+'A');
  105                     printf("%c", i+'A');
  106                 } /* if */
  107         } /* for */
  108     /* print out the lower case matches */
  109     for (i=26; i<52; i++)
  110         {
  111             /* for */
  112             if (mx == cnt[i])
  113                 {
  114                     /* if */
  115                     DEBUG printf("(%d,%d,%c)", i-26, i+'a'-26, i+'a'-26);
  116                     printf("%c", i+'a'-26);
  117                 } /* if */
  118         } /* for */
  119     /* print out the count */
  120     printf(" %d\n", mx);
  121 } /* FUNCTION process */
  122 
  123 int main()
  124 {
  125     /* main */
  126     int moreToDo;
  127 
  128     moreToDo = getInput();
  129     while (moreToDo)
  130         {
  131             /* while */
  132             process();
  133             moreToDo = getInput();
  134         } /* while */
  135 
  136     return EXIT_SUCCESS;
  137 } /* main */
  138