<mosaic.cnfolio.com>
PROGRAM
  1. /* Create needed variables
  2. Scan the first input and save it as the cipher length
  3. Error check this input to make sure it is within the specified limits
  4. Read each letter of the cipher text using a for loop, saving it to an array until the length of the cipher text is reached
  5. Error check the cipher text to make sure all characters are appropriate
  6. Scan the next input, and save it as the plain text length
  7. Error check this input to make sure it is within the specified limits
  8. Read each letter of the plain text using a for loop, saving each letter to an array until the length of the plain text is reached
  9. Error check the plain text to make sure all characters are appropriate
  10. Create a for loop that compares each letter of the plain text and cipher text
  11. Once a letter has been changed, use a variable to mark this. This will prevent a letter being changed more than once
  12. Print out which letter has been changed at each step
  13. Print out the changed word at each step */
  14.  
  15. #include <stdio.h>
  16.  
  17. int main ( void )
  18. {
  19.    int clength, plength, n, i;                                                 /* Creating all integer variables */
  20.    char ctext [ 17 ];                      /* Creating all character variables */
  21.    char ptext [ 257 ] = { '/0' };                                              /* Creating all character variables */
  22.    int changed [ 257 ] = { '0' };                                              /* Creating all integer variables */
  23.  
  24.  
  25.  
  26.    
  27.    scanf( "%d", &clength );                                                    /* Scanning the first input, saving it to the clength (cipher length) variable */ 
  28.  
  29.  
  30.    if(clength > 16)
  31.    {
  32.       printf( "Cipher text is too long. Please enter a maximum of 16 characters" ); /* Checks that the ctext is no longer than 16 characters */
  33.       return;
  34.    }
  35.    
  36.    if(clength < 2)
  37.    {
  38.       printf( "Cipher text is too short. Please enter a minimum of 2 characters" ); /* Checks that the ctext is no shorter than 2 characters */
  39.       return;
  40.    }
  41.  
  42.  
  43.  
  44.    for (n=0; n<=clength; n++)                  /* A loop that scans the cipher text until it equals the length of the cipher, */
  45.    {                                                   /* with n being the postition in the text */
  46.       scanf( "%c", &ctext[n] );                                 /* This saves each character of cipher text to an array called ctext */
  47.    }
  48.    
  49.    
  50.    for (n = 1; n <= clength - 1; n++)                             /* A for loop that scans through all but the last position of the ctext */
  51.    {
  52.     for (i = n + 1; i <= clength; i++)                                        /* A for loop that scans through all but the first position of the ctext */
  53.     {
  54.        if (ctext[n] == ctext[i])                                              /* This detects if two parts of the ctext are equal */
  55.          { 
  56.             printf("Duplciate found in positions %d and %d in ctext", n, i);   /* Informs the user of any duplicates and their positions in the ctext */
  57.             return;                                         /* Exits the program */
  58.          }
  59.     }
  60.    }
  61.    
  62.    
  63.    
  64.       for (n=1; n<=clength; n++)                 /* A loop that scans the cipher text until it equals the length of the text, with */
  65.    {                             /* n being the position in the text */
  66.       if (!(ctext[n] >= 'a' && ptext[n] <= 'z'))                               /* Checks each letter of ctext, making sure there are no invalid characters */
  67.       {
  68.          printf("Letter in position %d of cipher text is invalid. Please use lower case letters only.", n); /* Informs the user of the position of the invalid text */
  69.          return;                                                              /* Exits the program */            
  70.       }
  71.    }
  72.    
  73.      
  74.    scanf( "%d", &plength );                                                    /* Scans next line, and saves it as the length of the plain text */
  75.     
  76.    
  77.    if(plength > 256)
  78.    {
  79.       printf( "Plain text is too long. Please enter a maximum of 256 characters" ); /* Checks that the ptext is no longer than 256 characters */
  80.     return;                                                                        /* Exits the program */
  81.    }
  82.    
  83.    if(plength < 4)
  84.    {
  85.       printf( "Plain text is too short. Please enter a minimum of 4 characters" ); /* Checks that the ptext is no shorter than 4 characters */
  86.     return;                                                                       /* Exits the program */
  87.    }
  88.  
  89.  
  90.  
  91.    for (i=0; i<=plength; i++)                 /* A loop that scans the plain text until it equals the length of the text, with */
  92.    {                            /* i being the position in the text */
  93.       scanf( "%c", &ptext[i] );                                /* This saves each character of the plain text to an array called ptext */
  94.    }
  95.  
  96.  
  97.  
  98.    for (i=1; i<=plength; i++)                 /* A loop that scans the plain text until it equals the length of the text, with */
  99.    {                            /* i being the position in the text */
  100.       if (!(ptext[i] >= 'a' && ptext[i] <= 'z' || ptext[i] == ' '))           /* Checks each letter of ptext, making sure there are no invalid characters */
  101.       {
  102.          printf("Letter in position %d of plain text is invalid. Please use lower case letters and spaces only.", i); /*Informs the user of the position of the invalid text */
  103.          return;                                                                     /* Exits the program */
  104.       }
  105.    }
  106.  
  107.  
  108.  
  109.    for (n=1; n<=clength; n++)                    /* A for loop that repeats for each letter of cipher text */
  110.    {
  111.    
  112.       if (n == clength)   
  113.       {
  114.          printf("Step %d substitute %c with %c \n", n, ctext[n], ctext[1]);   /* This prints the final step of the encryption */
  115.       }
  116.    
  117.       else
  118.       {
  119.          printf("Step %d substitute %c with %c \n", n, ctext[n], ctext[n+1]); /* This prints all but the last step of encryption */
  120.       }
  121.      
  122.       for (i=1; i<=plength; i++)                /* A for loop that checks every letter of the plain text */
  123.       {
  124.  
  125.          if (ctext[n] == ptext[i] && changed[i] == 0)         /* If the current letter of ctext matches the ptext, and if it hasn't been changed already */
  126.          {
  127.             if (n == clength)                 /* If it is currentley checking the final letter of cipher text */
  128.             {
  129.                ptext[i] = ctext[1];                                           /* Change any letter of ptext equal to the last letter of ctext to the first letter of ctext */
  130.                changed[i] = 1;                                                /* Alter the changed variable to true (1) */
  131.             }
  132.            
  133.             else                         
  134.             {
  135.                ptext[i] = ctext[n+1];                            /* Otherwise, change any letter of ptext equal to the current ctext to the next letter of ctext */
  136.                changed[i] = 1;                                                /* Alter the changed variable to true (1) */
  137.             }
  138.          }   
  139.       }
  140.      
  141.       for (i=1; i<=plength; i++)
  142.       {
  143.          printf( "%c", ptext[i]);                                             /* For loop that prints the ciphered plain text at each step */
  144.        }   
  145.    
  146.       printf("\n");
  147.  
  148.    }
  149. }