Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/4/401/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 #define MAX_LINE 50
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-03-03
   20  * Purpose:
   21  * Problem: 401
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 char line[MAX_LINE];
   29 char tmp[MAX_LINE];
   30 char rev[MAX_LINE];
   31 char result[4][MAX_LINE] = { " -- is not a palindrome.",
   32                              " -- is a regular palindrome.",
   33                              " -- is a mirrored string.",
   34                              " -- is a mirrored palindrome."
   35                            };
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45 } /* FUNCTION dump */
   46 
   47 int getInput()
   48 {
   49     /* FUNCTION getInput */
   50     int dataReadFlag;
   51 
   52     fgets(line, MAX_LINE, stdin);
   53     if (feof(stdin))
   54         dataReadFlag = FALSE;
   55     else
   56         {
   57             /* something to read */
   58             dataReadFlag = TRUE;
   59             line[strlen(line)-1] = 0;
   60         } /* something to read */
   61     return (dataReadFlag);
   62 } /* FUNCTION getInput */
   63 
   64 int mirror()
   65 {
   66     /* FUNCTION mirror */
   67     int i;
   68     int goodMirror = TRUE;
   69 
   70     for (i=0; i<strlen(line); i++)
   71         {
   72             /* for */
   73             switch (line[i])
   74                 {
   75                 /* Switch */
   76                 case 'A':
   77                     tmp[i] = 'A';
   78                     break;
   79                 case 'E':
   80                     tmp[i] = '3';
   81                     break;
   82                 case 'H':
   83                     tmp[i] = 'H';
   84                     break;
   85                 case 'I':
   86                     tmp[i] = 'I';
   87                     break;
   88                 case 'J':
   89                     tmp[i] = 'L';
   90                     break;
   91                 case 'L':
   92                     tmp[i] = 'J';
   93                     break;
   94                 case 'M':
   95                     tmp[i] = 'M';
   96                     break;
   97                 case 'O':
   98                     tmp[i] = 'O';
   99                     break;
  100                 case 'S':
  101                     tmp[i] = '2';
  102                     break;
  103                 case 'T':
  104                     tmp[i] = 'T';
  105                     break;
  106                 case 'U':
  107                     tmp[i] = 'U';
  108                     break;
  109                 case 'V':
  110                     tmp[i] = 'V';
  111                     break;
  112                 case 'W':
  113                     tmp[i] = 'W';
  114                     break;
  115                 case 'X':
  116                     tmp[i] = 'X';
  117                     break;
  118                 case 'Y':
  119                     tmp[i] = 'Y';
  120                     break;
  121                 case 'Z':
  122                     tmp[i] = '5';
  123                     break;
  124                 case '1':
  125                     tmp[i] = '1';
  126                     break;
  127                 case '2':
  128                     tmp[i] = 'S';
  129                     break;
  130                 case '3':
  131                     tmp[i] = 'E';
  132                     break;
  133                 case '5':
  134                     tmp[i] = 'Z';
  135                     break;
  136                 case '8':
  137                     tmp[i] = '8';
  138                     break;
  139                 default:
  140                     goodMirror = FALSE;
  141                     tmp[i] = 0;
  142                     i = MAX_LINE - 1;
  143                     break;
  144                 } /* Switch */
  145         } /* for */
  146     tmp[i] = 0;
  147     return goodMirror;
  148 } /* FUNCTION mirror */
  149 
  150 void reverse(char buff[])
  151 {
  152     /* FUNCTION reverse */
  153     int i;
  154     int j;
  155     int ln;
  156 
  157     ln = strlen(buff);
  158     for (i=0, j=ln-1; i<ln; i++, j--)
  159         {
  160             /* for */
  161             rev[j] = buff[i];
  162         } /* for */
  163     rev[ln] = 0;
  164 } /* FUNCTION reverse */
  165 
  166 void process()
  167 {
  168     /* FUNCTION process */
  169     int palFlag;
  170     int mirFlag;
  171     int idx = 0;
  172 
  173     DEBUG printf("line=[%s]\n", line);
  174     reverse(line);
  175     DEBUG printf(" rev=[%s]\n", rev);
  176     palFlag = (0 == strcmp(line, rev));
  177     mirFlag = mirror();
  178     if (mirFlag)
  179         {
  180             /* was able to miror the string */
  181             DEBUG printf(" tmp=[%s]\n", tmp);
  182             mirFlag = (0 == strcmp(tmp, rev));
  183         } /* was able to miror the string */
  184     if (palFlag)
  185         {
  186             idx = idx + 1;
  187         }
  188     if (mirFlag)
  189         {
  190             idx = idx + 2;
  191         }
  192     printf("%s%s\n\n", line, result[idx]);
  193 } /* FUNCTION process */
  194 
  195 int main()
  196 {
  197     /* main */
  198     int moreToDo;
  199 
  200     init();
  201     moreToDo = getInput();
  202     while (moreToDo)
  203         {
  204             /* while */
  205             process();
  206             moreToDo = getInput();
  207         } /* while */
  208 
  209     return EXIT_SUCCESS;
  210 } /* main */
  211