<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. -Pseudo-
  3. Start
  4. Pick a character, must be 2< and >16.
  5. All characters must be in lower case.
  6. No character duplicates are allowed.
  7. Select a portion of text to encrypt, this must be 4< >256 charactors long.
  8. All characters must be in lower case.
  9. Use the character "N" to highlight the current position of the cypher key.
  10. Starting position is indicated = (N=1)
  11.  
  12. */
  13. #include<stdio.h>
  14.  
  15.  
  16. int main( void )
  17. {
  18.   char cipherkey[16];
  19.   char text[255];
  20.   int letterswap[255] = {0};
  21.   int cipherkeynumber, plaintextnumber,letterError,i,j,k,g;
  22.  
  23.   scanf("%d", &cipherkeynumber);
  24.   if (cipherkeynumber>16){            /* if cipher is bigger than 16, display error message */   
  25.   printf("Cipher key is too large");
  26.   }
  27.   if (cipherkeynumber<2){                        /* if cipher is lower than 2, display error message */
  28.   printf("Cipher key is too small");
  29.   }
  30.   else{
  31.     scanf("%c",&text[0]);
  32.     for (i = 0; i < cipherkeynumber; i++)
  33.     {
  34.       scanf("%c", &cipherkey[i]);
  35.       for(j=0;j<cipherkeynumber;j++)
  36.          {
  37.       if (cipherkey[i]==cipherkey[j] && i!=j){          /* Checks cipher key, to make sure that the cipherkeys letters are not the same. This avoids duplicates within the cipherkey  */
  38.      
  39.      
  40.       letterError=1;
  41.       printf("\n%c is a duplicated letter, this is classed as an error in the cipher key", cipherkey[i]);
  42.     }
  43.     }
  44.     }   
  45.     scanf("%d", &plaintextnumber);
  46.     if (plaintextnumber>256){           /* if plaintext is larger than the maximum value then display error message */
  47.       printf("Plain text is too large");
  48.     }
  49.     if (plaintextnumber<4){                                 /* if plaintext is smaller than the minimum value then display error message */
  50.     printf("Plain text is too small");
  51.     }
  52.     else{
  53.      
  54.      
  55.       /* Using false variable to allow the programme to read the enter as a space */
  56.       scanf("%c", &text[0]);
  57.       for (i = 0; i < plaintextnumber; i++)
  58.       {
  59.         scanf("%c", &text[i]);
  60.         if (text[i] != 32){                              /* if text does not equal 32 then display error message */
  61.         if (text[i] < 97){           /* if text is smaller than 97 then display error message */
  62.         printf("Character not valid");
  63.         }
  64.         if (text[i] > 122){                              /* if text is larger than 122 then display error message */
  65.         printf("Character not valid");
  66.         }
  67.         }
  68.         }
  69.       printf("\n");
  70.      
  71.       /*changed array is implemented*/
  72.       for (i = 0; i < cipherkeynumber; i++){
  73.         for (j = 0; j < plaintextnumber; j++){
  74.           if (letterswap[j] == 0){
  75.             if (i == cipherkeynumber - 1){
  76.               if (text[j] == cipherkey[i]){
  77.               text[j] = cipherkey[0];
  78.               letterswap[j] = 1;
  79.             }
  80.             }
  81.             else{
  82.               if (text[j] == cipherkey[i]){
  83.                 text[j] = cipherkey[i+1];
  84.                 letterswap[j] = 1;
  85.               }
  86.             }
  87.           }
  88.         }
  89.       if (i == cipherkeynumber - 1){                                                   
  90.         printf("Step %d substitute %c with %c\n", (i+1), cipherkey[i], cipherkey[0]);
  91.       }
  92.       else {
  93.         printf("Step %d substitute %c with %c\n", (i+1), cipherkey[i], cipherkey[i+1]);
  94.       }
  95.       for (k = 0; k < plaintextnumber; k++){
  96.         printf("%c", text[k]);
  97.       }
  98.       printf("\n");
  99.       }
  100.     }
  101.   }
  102.  
  103. }