<mosaic.cnfolio.com>
PROGRAM
  1. //------------------PSUEDO CODE ---------------------------------------------------------------------------------------------
  2. /* Get the amount of characters of cipher key
  3.    Check the amount of characters is between 2-16
  4.    Get characters of cipher key
  5.    Check all characters are lowercase
  6.    
  7.    Get the amount of characters for plain text
  8.    Check the amount is between 4- 236
  9.    Get characters of the plain text
  10.    Check all the characters are lowercase and spaces
  11.    Set position number N in the cipher key to equal 0
  12.    
  13.    WHILE N is smaller than the number of characters in the cipher key
  14.      Name the character at position N the target value
  15.      Add 1 to N
  16.      Name the character at position N the substitution value
  17.      Replace all target values found with the substitution value
  18.      Print the output
  19.    END WHILE
  20.    END */
  21.    
  22. //---------------------------------------------------------------------------------------------------------   
  23.    
  24.  #include<stdio.h>
  25.  #include<string.h>
  26.  
  27.  int main (void)
  28.  {
  29.    int cipherX; // variable for the number of character of the cipher key
  30.    scanf( "%d", &cipherX); //reading the input to get the number of characters for the cipher key
  31.  while (( cipherX < 2) || (cipherX > 16)) // error checking to make sure the length of the cipher key is between 2 and 16 characters
  32.  {
  33.   printf(" please input cipher key between 2 and 16 characters "); // error message incase illegal values are entered
  34.   exit(1); // exits the program to show error message
  35.   }
  36.  
  37.   int counter1;
  38.   char key[16]={'\0'}; // initialising the string for the cipher key
  39.   for ( counter1=0; counter1 <= cipherX; counter1++){
  40.    scanf("%c", &key[counter1]);
  41.    if(isupper(key[counter1])){ // checks that the cipher key contains no uppercase characters
  42.    printf( "please only use lowercase for your cipher key ");
  43.    exit(1); //exits the program to show error message
  44.   }
  45.   }
  46.    
  47. //---------------------------------------------------------------------------------------------------------------------------------------   
  48.    
  49.   int plainX; // variable for the number of characters in the plain text
  50.   scanf( "%d", &plainX); // reading the input to get the number of characters in the plain text
  51.  while (( plainX < 4 ) || (plainX > 236)) // checking the length of the plain text is between 4 and 236 characters
  52. {
  53.  printf(" please input plain text between 4 and 236 characters ");
  54.  exit(1); //exits the program to show error message
  55.  }
  56.  
  57.  int counter2;
  58.  char plain[236]={'\0'}; // initialising the string for the plain text
  59.  for ( counter2=0; counter2 <= plainX; counter2++ ){
  60.    scanf("%c", &plain[counter2]);
  61.     if((!islower(plain[counter2])&&(!isspace(plain[counter2])))){ // check that the plain text only contains spaces and lowercase
  62.    printf( " please only use lowercase and spaces " );
  63.    exit(1); //exits the program to show error message
  64.    }
  65.  }
  66.  
  67.  //--------------------------------------------------------------------------------------------------------------------------------------
  68.  
  69.  int N=1; // setting the pointer to the first position in the cipher key
  70.  int p=0; // plain text pointer
  71.  int step=1;
  72.  
  73. while (N<= cipherX -1){
  74. for ( p=0; p <= plainX; p++){ // running through the characters of the plain text
  75.  if( key[N] == plain[p] ){ //finding target values in the plain text
  76.    plain[p] = key[N+1]; // swapping the target value for the substitution value
  77.    printf("step %d \n", step); // output what step it is
  78.    printf("replace %c with %c ", key[N], key[N+1]); // output the target value and substitution value of the cipher key
  79.    printf("%s\n", plain);
  80.    step ++; //increment the step number
  81.    N ++; //increment the position in the cipher key
  82.  } 
  83.  } 
  84. }
  85.  
  86. for ( p=0; p <= plainX; p++){ 
  87.  if( key[N] == plain[p] ){ // checks if the cipher key character and plain text charcter are the same
  88.    plain[p] = key[1]; // swapping the last letter in the cipher key with the first letter in the cipher key
  89.    printf("step %d \n", step); //  prints the step number
  90.    printf("replace %c with %c ", key[N], key[1]); // prints target value and swap value
  91.    printf("%s\n", plain); // prints the new string
  92.    step ++;
  93.    N ++;
  94. }
  95. }
  96. }