Computer Programming Contest Preparation

ToolBox - Source for: 6/641/a.c



/home/toolbox/public_html/solutions/6/641/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2021-12-13
   19  * Purpose: fun
   20  * Problem: 641 - Do the Untwist
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_LINE 100
   28 
   29 int key;
   30 char line[MAX_LINE];
   31 char msg[MAX_LINE];
   32 int  slen;
   33 char int2char[29];
   34 
   35 void init()
   36 {
   37     /* FUNCTION init */
   38     strcpy(int2char, "_abcdefghijklmnopqrstuvwxyz.");
   39 } /* FUNCTION init */
   40 
   41 void dump()
   42 {
   43     /* FUNCTION dump */
   44 } /* FUNCTION dump */
   45 
   46 int getInput()
   47 {
   48     /* FUNCTION getInput */
   49     int dataReadFlag;
   50 
   51     scanf(" %d ", &key);
   52     dataReadFlag = (0 != key);
   53     if (dataReadFlag)
   54         {
   55             /* get encrypted string */
   56             scanf(" %s ", line);
   57             slen = strlen(line);
   58         } /* get encrypted string */
   59     return (dataReadFlag);
   60 } /* FUNCTION getInput */
   61 
   62 int char2int(char chr)
   63 {
   64     /* FUNCTION char2int */
   65     int toReturn;
   66 
   67     if ('_' == chr)
   68         {
   69             toReturn = 0;
   70         }
   71     else if ('.' == chr)
   72         {
   73             toReturn = 27;
   74         }
   75     else
   76         {
   77             toReturn = chr - 'a' + 1;
   78         }
   79     return toReturn;
   80 } /* FUNCTION char2int */
   81 
   82 void process()
   83 {
   84     /* FUNCTION process */
   85     int i;
   86     int tmp;
   87     int idx;
   88 
   89     msg[slen] = 0;
   90     for (i=0; slen>i; i++)
   91         {
   92             /* for each cahr */
   93             idx = (key * i) % slen;
   94             tmp = char2int(line[i]);
   95             msg[idx] = int2char[(tmp + i) % 28];
   96             DEBUG printf("(line[%d] = %c) (key %d) (tmp %d) (msg[%d] %d)\n", i, line[i], key, tmp, idx, msg[idx]);
   97         } /* for each cahr */
   98     printf("%s\n", msg);
   99 
  100 
  101 } /* FUNCTION process */
  102 
  103 int main()
  104 {
  105     /* main */
  106     int moreToDo;
  107 
  108     init();
  109     moreToDo = getInput();
  110     while (moreToDo)
  111         {
  112             /* while */
  113             process();
  114             moreToDo = getInput();
  115         } /* while */
  116 
  117     return EXIT_SUCCESS;
  118 } /* main */
  119