<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.  
  4.     Scan cipher key length
  5.     Scan cipher key
  6.     Scan text length
  7.     Scan text
  8.    
  9.     error = 0
  10.    
  11.     IF cipher key length < 2 or > 16
  12.    
  13.       print "cipher key must be between 2 and 16 charecters"
  14.       error = 1
  15.    
  16.     END IF
  17.    
  18.     IF text legnth < 4 or > 256
  19.    
  20.       print "text must be between 4 and 256 characters"
  21.       error = 1
  22.    
  23.     END IF
  24.    
  25.     IF error = 0
  26.    
  27.       n1 = 0
  28.       n2 = 0
  29.      
  30.       WHILE n1 less than cipher length
  31.      
  32.         n1th letter of cipher = target
  33.         (n+1)th letter of cipher = subtitute
  34.        
  35.         WHILE n2 not equal to text length
  36.        
  37.           check = n2th letter of text
  38.          
  39.           IF check = target
  40.          
  41.             n2th letter of text = subtitute
  42.          
  43.           END IF
  44.          
  45.           increase n2 by 1
  46.        
  47.         END WHILE
  48.        
  49.         n2 = 0
  50.         increase n1 by 1
  51.      
  52.       END WHILE
  53.      
  54.       print text
  55.    
  56.     END IF
  57.  
  58. END
  59. */
  60.  
  61. #include <stdio.h>
  62. #include <string.h>
  63. int main(void)
  64.   {
  65.     // Main Variables
  66.     int keyLength = 0;
  67.     char cipher[16] = {'\0'};
  68.     int textLength = 0;
  69.     char inputText[256] = {'\0'};
  70.    
  71.     // Error checking
  72.     int error = 0;
  73.    
  74.     // Reading main variables
  75.     scanf("%d",&keyLength);
  76.     scanf("%s",cipher);
  77.     scanf("%d",&textLength);
  78.     scanf("\n %256c",inputText);
  79.    
  80.     // Checking cipher length is valid
  81.     if((keyLength < 1)||(keyLength > 17))
  82.     {
  83.       printf("Cipher Key must be between 2 and 16 charecters \n");
  84.       error = 1;
  85.     }
  86.    
  87.     // Checking inputted cipher key length is correct
  88.     if(keyLength != strlen(cipher))
  89.     {
  90.       printf("Cipher is incorect length \n");
  91.       error = 1;
  92.     }
  93.    
  94.     // Checking input text length is valid
  95.     if((textLength < 3)||(textLength > 257))
  96.     {
  97.       printf("Text must be between 4 and 256 charecters \n");
  98.       error = 1;
  99.     }
  100.    
  101.     // Checking inputted text length is correct
  102.     if(textLength != strlen(inputText))
  103.     {
  104.       printf("Text is incorrect length \n");
  105.       error = 1;
  106.     }
  107.    
  108.     // Checking for errors
  109.     if(error == 0)
  110.     {
  111.       // Charecters for subtitution
  112.       char target = {'\0'};
  113.       char sub = {'\0'};
  114.       char check = {'\0'};
  115.      
  116.       // charecter counter for cipher
  117.       int n1 = 0;
  118.       int n2 = 0;
  119.      
  120.       // Setting target and subtitute
  121.       while(n1 != keyLength - 1)
  122.       {
  123.         target = cipher[n1];
  124.         sub = cipher[n1+1];
  125.        
  126.         // Setting check charecter
  127.         while(n2 != textLength)
  128.         {
  129.           check = inputText[n2];
  130.          
  131.           // subtituting text if target is found
  132.           if(target == check) inputText[n2] = sub;
  133.          
  134.           // moving to next charecter in text
  135.           n2++;
  136.         }
  137.        
  138.         // reseting text counter
  139.         n2 = 0;
  140.        
  141.         // moving to next letter of cipher
  142.         n1++;
  143.       }
  144.      
  145.       // Setting subtitute to first letter of cipher when target is last letter of cipher
  146.       if(n1 == keyLength)
  147.       {
  148.         target = cipher[n1];
  149.         sub = cipher[0];
  150.        
  151.         // subtituting text if target is found
  152.         if(target == check) inputText[n2] = sub;
  153.       }
  154.       // Outputting text
  155.       printf("%s \n", inputText);
  156.     }
  157.   }