<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.  
  4. Get first line of input and store as cipher key length
  5.  
  6. IF cipher key length is not between 2 and 16
  7.    THEN throw up and error message
  8.  
  9. Get second line of input, store as cipher key
  10.  
  11. IF cipher key is not the same length as cipher key length
  12.    THEN throw up an error message
  13. END IF
  14.  
  15. IF cipher key is not all valid characters
  16.    THEN throw up an error message
  17. END IF
  18.  
  19. Get third line of input, store as plain text length
  20.  
  21. IF its not between 4 and 256
  22.    THEN throw up an error message
  23. END IF
  24.  
  25. IF plain text is not all valid characters
  26.    THEN throw up an error message
  27. END IF
  28.  
  29. Get fourth line of input, store as plain text
  30.  
  31. IF not between 4 and 256
  32.    THEN throw up an error message
  33. END IF
  34.  
  35.  
  36. Define current position in cipher as N. This is the target value
  37. Make a copy of plain text, store as copy
  38.  
  39. WHILE we still have cipher key letters remaining
  40.    display step number, and what we are doing
  41.    
  42.    WHILE we still have plain text letters remaining
  43.       IF current plain text letter is the same as cipher N
  44.          THEN replace corresponding copy text letter with cipher N+1
  45.       END IF
  46.    END WHILE
  47.  
  48. END WHILE
  49.  
  50. END
  51. */
  52.  
  53. #include <stdio.h>
  54. #include <string.h>
  55.  
  56. int main(void)
  57.  
  58. {
  59.    //setting variables
  60.    int cipherkeylength, plaintextlength, N;
  61.    char cipherkeytext [16], plaintext [256], cipheredtext [256], Targetvalue, substitutionvalue;
  62.    
  63.    
  64.    //checking inputs
  65.    scanf("%d", &cipherkeylength);
  66.    scanf("%s", &cipherkeytext);
  67.    scanf("%d", &plaintextlength);
  68.    scanf(" %[^\t\n]s", &plaintext);
  69.    
  70.    
  71.    //check that the inputs are in the allowed range
  72.    if ((cipherkeylength>16) || (cipherkeylength <2) || (cipherkeylength != strlen(cipherkeytext)) ||
  73.    (plaintextlength >256) || (plaintextlength <4) || (plaintextlength != strlen(plaintext)))
  74.    {
  75.       printf("your values are wrong, check the range allowed");
  76.    }
  77.    else{
  78.       printf("inputs are vaild \n");
  79.        }
  80.      
  81.       strcpy(cipheredtext, plaintext); //make copy of plaintext and store as copy
  82.    
  83.   for (N=0; N < cipherkeylength; N++) //using n=0, n=1 was not possible
  84.    {
  85.    
  86.       Targetvalue= cipherkeytext [N]; //setting N as the target value
  87.       printf("The target value is %c \n",Targetvalue);
  88.    
  89.       if(N+1 < cipherkeylength)
  90.       {
  91.           substitutionvalue= cipherkeytext [N+1]; //setting N+1 as substitution value
  92.        }
  93.        else
  94.        {
  95.          substitutionvalue= cipherkeytext [0];
  96.        }
  97.      
  98.       printf("The substitution value is %c \n",substitutionvalue);
  99.      
  100.       for(int i=0; i<plaintextlength; i++)
  101.       {
  102.          if(plaintext[i] == Targetvalue)
  103.          {
  104.             cipheredtext[i] = substitutionvalue;
  105.          }
  106.       }
  107.      
  108.       printf("Ciphered text: %s \n\n",cipheredtext); //print ciphered text
  109.    
  110.    }
  111. }