<mosaic.cnfolio.com>
PROGRAM
  1. /* Pseudocode:
  2.  
  3.       START
  4.             Declare the variable 'Cipher key'
  5.             Declare the variable 'Plain text'
  6.          
  7.       IF    Cipher key >=2 AND <=16
  8.          THEN
  9.             Store the value of 'Cipher key'
  10.       ELSE  Display message, "Cipher key value must be between 2 and 16. Please try again."
  11.             Return to start of program
  12.       END IF
  13.      
  14.       IF    Cipher key contains no duplicate characters AND lowercase characters
  15.          THEN
  16.             Store the string of the 'Cipher key'
  17.       ELSE  Display message, "Cipher key must have no duplicate characters and needs to be all lowercase. Please try again."
  18.             Re-enter the string of 'Cipher key'           
  19.       END IF
  20.      
  21.       IF    Plain text >=4 AND <=256
  22.          THEN
  23.             Store the value of 'Plain text'
  24.       ELSE  Display message, "Plain text value needs to be between 4 and 256. Please try again."
  25.             Re-enter the value of 'Plain text'
  26.       END IF
  27.       
  28.       IF  Plain text contains spaces AND lowercase characters
  29.          THEN
  30.             Store the string of the 'Plain text'
  31.       ELSE  Display message, "Plain text needs to contain spaces and be all lowercase."
  32.             Re-enter the string of 'Plain text.'
  33.            
  34.             Assign 'N' to indicate current position in cipher key (N = 1 is 1st position)
  35.            
  36.             Compare characters in 'Cipher text' to 'Plain text'
  37.       IF    Cipher text characters = Plain text characters
  38.          THEN
  39.             Change target value with the subsitution value
  40.       ELSE IF
  41.             Loop until next each character is compared     
  42.       END IF
  43.       END     
  44. */
  45.  
  46.  
  47. #include <stdio.h>
  48.  
  49. int main(void)
  50. {
  51.   //Declare two integers, a and b, to hold the specified length of the plaintext and ciphertext.
  52.   int a, b;
  53.   //retrieve input.
  54.   scanf ("%d", &a);
  55.   //declare char array to hold the ciphertext string. Length one greater than input to hold whitespace char.
  56.   char cipher[a + 1];
  57.   //retrieve input.
  58.   scanf ("%s", &cipher);
  59.   //retrieve input.
  60.   scanf ("%d", &b);
  61.   //declare two char arrays, one to hold the plain text, the other will be the encrypted result.Each of length one greater than input to hold whitespace char.
  62.   char plain[b + 1];
  63.   char result[b + 1];
  64.   //retrieve input.
  65.   scanf (" %[^\n]s", &plain);
  66.  
  67.   //Set result array equal to plaintext
  68.   for (int k = 0; k < b + 1; k++)
  69.     {
  70.       result[k] = plain[k];
  71.     }
  72.   //integers to hold current position and number of current changes (ie. current step)
  73.   int j;
  74.   int count = 1;
  75.   //Nested for loop to cycle through each char in cipher text, compare to each char in plain text and change if neccessary.
  76.   for (j = 0; j < a; j = j + 1)
  77.     {
  78.       //print out the step
  79.       printf ("Step ");
  80.       printf ("%d", count);
  81.       printf (" substitute ");
  82.       printf ("%c", cipher[j]);
  83.       printf (" with ");
  84.  
  85.       if (j == a - 1)
  86.    {
  87.      printf ("%c", cipher[0]);
  88.    }
  89.       else
  90.    {
  91.      printf ("%c", cipher[j + 1]);
  92.    }
  93.  
  94.  
  95.       for (int i = 0; i < b; i = i + 1)
  96.    {
  97.      //Compare current j value to each i value (ie. compare current ciphertext char to each plaintext char)
  98.      //Chnage result array if a match is found. Keep plaintext the same.
  99.      if (plain[i] == cipher[j] && j < a - 1)
  100.        {
  101.          result[i] = cipher[j + 1];
  102.        }
  103.      else if (plain[i] == cipher[j] && j == a - 1)
  104.        {
  105.          result[i] = cipher[0];
  106.        }
  107.  
  108.    }
  109.       //Print out current result and increment count.
  110.       printf ("\n");
  111.       printf (&result);
  112.       printf ("\n");
  113.       count = count + 1;
  114.     }
  115.  
  116.   return 0;
  117. }