<mosaic.cnfolio.com>
PROGRAM
  1. #include <stdio.h>
  2.  
  3. /*
  4.    --- Pseudo Code ---
  5.  
  6. initialise variables
  7.  
  8. read input for key length
  9. store key length in variable
  10.  
  11. IF key length is <2 or >16
  12.    display error message
  13.    stop program
  14. END IF
  15.  
  16. create cipher key array that is same size as key length variable
  17.  
  18. read cipher key string
  19. store cipher key string in array
  20.  
  21. IF length of cipher key string is not equal to key length variable
  22.    display error message
  23.    stop program
  24. END IF
  25.  
  26. read input for plain text length
  27. store plain text length in variable
  28.  
  29. IF plain text length is <4 or >256
  30.    display error message
  31.    stop program
  32. END IF
  33.  
  34. create plain text array that is same size as plain text length variable
  35.  
  36. read plain text string
  37. store plain text string in array
  38.  
  39. IF length of plain text string is not equal to plain text length variable
  40.    display error message
  41.    stop program
  42. END IF
  43.  
  44. set counter1 to 0
  45. set counter2 to 0
  46.  
  47. WHILE counter1 is less than length of cipher key
  48.  
  49.    output step number and what character will be replaced with what
  50.  
  51.    WHILE counter2 is less than length of plain text
  52.      IF corresponding character to counter1 value is found in plaintext
  53.         replace that character with character in cipher key that corresponds to counter1 + 1
  54.      END IF
  55.      
  56.      IF counter is equal to cipher key length
  57.          use character at start of cipher key instead of counter + 1
  58.          output step number and what character was replaced with what
  59.          output new plaintext string
  60.          break out of loop
  61.      END IF
  62.      
  63.      increase counter2 by 1
  64.    
  65.    END WHILE
  66.    
  67.    output new plaintext string
  68.    increase counter1 by 1
  69.    
  70. END WHILE
  71.    
  72.    
  73.  
  74.  
  75.  
  76. */
  77.  
  78. int main( void ){
  79.  
  80. int a; /* Counter for loops */
  81. int n; /* Current position in cipher key */
  82. int keylength; /* Stores length of cipher key */
  83. int textlength; /* Stores length of plain text */
  84.  
  85. scanf("%d", &keylength); /* Reads length of cipher key and stores in variable */
  86.  
  87. /* Checks length of cipher key, gives error message + stops program if not within limits */
  88. if(keylength < 2 || keylength > 16){
  89.    printf("ERROR: Please enter a key length that is between 2 and 16.");
  90.    exit(0);
  91.    }
  92.  
  93. char cipherkey[keylength - 1]; /* Initializes cipher key array, -1 is because it starts on 0 instead of 1 */
  94.  
  95. scanf("%s", &cipherkey); /* Reads cipher key string and stores in array */
  96.  
  97. /* Gives error message and stops program if cipher key does not match length that was inputted */
  98. if(strlen(cipherkey) != keylength){
  99.    printf("ERROR: The length of the cipher key (Input line 2) does not match the length given (Input line 1).\n");
  100.    exit(0);
  101.    }
  102.  
  103. scanf("%d", &textlength); /* Reads length of plaintext and stores in variable */
  104.  
  105. /* Checks length of plaintext, gives error message + stops program if not within limits */
  106. if(textlength < 4 || textlength > 256){
  107.    printf("ERROR: Please enter a plain text length that is between 4 and 256.");
  108.    exit(0);
  109.    }
  110.  
  111. char plaintext[textlength - 1]; /* Initializes plaintext array, -1 is because it starts on 0 instead of 1 */
  112.  
  113. scanf(" %[^\n]s", &plaintext); /* Reads plaintext and stores in array */
  114.  
  115. /* Gives error message and stops program if plain text does not match length that was inputted */
  116. if(strlen(plaintext) != textlength){
  117.    printf("ERROR: The length of the plain text (Input line 4) does not match the length given (Input line 3).\n");
  118.    exit(0);
  119.    }
  120.  
  121. printf("Original text: %s\n", plaintext); /* Prints original text before it is changed */
  122.  
  123. int changed [textlength - 1]; /* Initializes changed array for storing which plaintext values have been modified */
  124.  
  125. for(a=0; a < textlength; a++){ /* Sets all values in changed array to 0 */
  126.    changed[a] = 0;
  127.    }
  128.  
  129. for(n=0; n < keylength; n++){ /* Repeats until it has gone through cipher key */
  130.    
  131.    if(n == keylength - 1){ /* Outputs which character will be replaced with which for each step */
  132.       printf("Step %d: Replace %c with %c.\n", n+1, cipherkey[n], cipherkey[0]);
  133.          }
  134.       else{
  135.       printf("Step %d: Replace %c with %c.\n", n+1, cipherkey[n], cipherkey[n+1]);
  136.          }
  137.          
  138.    for(a=0; a < textlength; a++){ /* Repeats until it has gone through plain text */
  139.       if(n == (keylength - 1) && plaintext[a] == cipherkey[n] && changed[a] == 0){ /* Runs when at end of cipher key */
  140.          plaintext[a] = cipherkey[0]; /* Replaces character at end of plain text with the character at the start */
  141.          changed[a] = 1; /* Sets corresponding value in changed array to 1 so it is not changed again */
  142.          }
  143.          else if(plaintext[a] == cipherkey[n] && changed[a] == 0){ /* Runs when at any other position that is not end of cipher key */
  144.             plaintext[a] = cipherkey[n+1]; /* Replaces character with next character in cipher key */
  145.             changed[a] = 1; /* Sets corresponding value in changed array to 1 so it is not changed again */
  146.             }
  147.       }
  148.      
  149.    printf("%s\n", plaintext); /* Prints new plaintext after each step */
  150.    
  151.    }
  152.  
  153. printf("New text: %s\n", plaintext); /* Outputs changed text after program has changed the original text */
  154.  
  155. }