<mosaic.cnfolio.com>
PROGRAM
  1. /* ----------------------------------------------------- PseudoCode  ----------------------------------------------------- */
  2.  
  3.  
  4. /*
  5. START
  6. Variables for various objects in use in my code
  7.  
  8. Scan for inputs
  9. - Cipher Length
  10. - Cipher
  11. - Text Length
  12. - Text
  13.  
  14.  
  15. Error check
  16. (If statements to check length of texts as well as for loops for the cipher and text)
  17. - Make sure the cipher is between 2 and 16
  18. - Make sure the text is between 4 and 256
  19. - Make sure cipher and cipher length match up
  20. - Make sure text and text length match up
  21. - Make sure cipher has no duplicates
  22. - Make sure only lower case text is used
  23. - Make sure fields aren’t empty
  24.  
  25. Make each error that is true increase a flag count by 1, to be used later
  26.  
  27. Copy the first character to the end of the Cipher string to enable the last letter to replace the letter before it (if found in the text)
  28. (strncat command, used to define the specified character I want to copy)
  29. Copy the Text into another field which we use to modify in the next step, therefore eliminating the issue of already replaced letters getting replaced in a loop
  30. (strcopy can be used as we are copying the entire string)
  31.  
  32. IF Statement to check whether the flag counter = 0, if not, don’t run the below code and just end, printing off the above error comments
  33.  
  34. Perform a FOR loop within a FOR loop to loop the cipher and text string arrays as appropriate
  35. When it finds a match in the text array and cipher array, modify the specified character found within the copied string (from above) with the n + 1 cipher
  36. Increase step count by one
  37.  
  38. Print the result with each step, displaying which letters have been replaced with which
  39.  
  40. END
  41.  
  42. */
  43.  
  44.  
  45.  
  46. /* ----------------------------------------------------- Main Body  ----------------------------------------------------- */
  47.  
  48.  
  49. int main()
  50. {
  51.  
  52.  
  53. /* ----------------------------------------------------- Variables  ----------------------------------------------------- */
  54.  
  55.   int cipherLength = 0, plaintextLength = 0/* Main variables I need for data entry */   
  56.   char Cipher[17], plainText[256], encryptedText[256];   /* All the Character segments I will require, including the array sizes, the Cipher is 17 instead of 16 due to the workaround I have done for comparing the last value */
  57.   int cipherCurrent = 0, plaintextCurrent = 0, turnCounter = 0;   /* All the status checkers, the cipher array/text array/turn counters */
  58.   int cipherlengthCheck = 0, plaintextlengthCheck = 0; /* Used to error check the length of the two entries compared to the number given */
  59.   int errorCount = 0; /* This is the flag counter for the error checking */
  60.   int j; /* Various non-named looping pointers */
  61.  
  62. /* ----------------------------------------------------- Inputs  ----------------------------------------------------- */
  63.  
  64.  
  65.   scanf("%d", &cipherLength);
  66.   scanf("%s", &Cipher);
  67.   scanf("%d", &plaintextLength);
  68.   scanf(" %[^\n]s", &plainText);
  69.   strcpy (encryptedText, plainText); /* This copies the main plainText into a temporary variable which we can manipulate without affecting the main string */
  70.  
  71.  
  72.  
  73.  
  74. /* -------------------------------------------------------- Error checking -------------------------------------------------------- */
  75.  
  76.  
  77.    
  78.  
  79. /* Check if the input for the cipher key is all letters */
  80.     for(cipherCurrent = 0; cipherCurrent < cipherLength; cipherCurrent++)
  81.         {
  82.                 if(isalpha(Cipher[cipherCurrent]) == 0) /* This checks if the inputted value is between a and z, if not it rejects it */
  83.                 {
  84.                     printf("You have entered an invalid entry in the cipher field, you are only allowed to use lowercase letters\n");
  85.                     errorCount++;
  86.                     cipherCurrent = cipherLength;
  87.                 }
  88.         }   
  89.        
  90.        
  91.        
  92.        
  93.  
  94. /* Check for plaintext being all letters */
  95.     for(plaintextCurrent = 0; plaintextCurrent < plaintextLength; plaintextCurrent++)
  96.         {
  97.             if(isalpha(plainText[plaintextCurrent]) == 0 && plainText[plaintextCurrent] != ' ')
  98.                 {
  99.                     printf("You have entered an incorrect entry in the text field, you are only allowed to use lowercase letters\n");
  100.                     errorCount++;
  101.                     plaintextCurrent = plaintextLength;
  102.                 }
  103.         }
  104.      
  105.      
  106. /* Check if cipherLength is between 2 and 16 */
  107.   if (cipherLength < 2)
  108.     {
  109.       printf ("Your cipher length is too short, it must be greater than or equal to 2.\n");
  110.       errorCount++;
  111.     }
  112.    
  113.   if (cipherLength > 16)
  114.     {
  115.       printf ("Your cipher length is too long, it must be less than or equal to 16.\n");
  116.       errorCount++;
  117.     }   
  118.  
  119.  
  120.  
  121. /* Check if plaintextLength is between 4 and 256 */
  122.   if (plaintextLength < 4)
  123.     {
  124.       printf ("Your text length is too short, it must be greater than or equal to 4.\n");
  125.       errorCount++;
  126.     }
  127.    
  128.   if (plaintextLength > 256)
  129.     {
  130.       printf ("Your text length is too long, it must be less than or equal to 256.\n");
  131.       errorCount++;
  132.     }
  133.    
  134.  
  135.  
  136.  
  137.  
  138. /* check if cipherLength and Cipher match up */
  139.   cipherlengthCheck = strlen(Cipher); /* puts the length of the string into the cipherlengthCheck variable to be checked */
  140.    if(cipherLength != cipherlengthCheck)
  141.       {
  142.          printf("The number you have entered for your cipher does not match up to the amount of characters entered in your cipher.\n");
  143.          errorCount++;
  144.       }
  145.      
  146.      
  147. /* Check if plaintextLength and plainText match up */
  148.   plaintextlengthCheck = strlen(plainText)/* puts the length of the string into the plaintextlengthCheck variable to be checked */
  149.    if(plaintextLength != plaintextlengthCheck)
  150.       {
  151.          printf("The number you have entered for your text does not match up to the amount of characters entered in your text.\n");
  152.          errorCount++;
  153.       }
  154.            
  155.  
  156.  
  157.  
  158.  
  159. /* Check for duplicates in the Cipher */
  160.     for(cipherCurrent = 0; cipherCurrent < cipherLength; cipherCurrent++)
  161.         {
  162.             for(j = cipherCurrent + 1; j < cipherLength + 1; j++)
  163.                 {
  164.                     if(Cipher[cipherCurrent] == Cipher[j])
  165.                         {
  166.                             printf("letters %d (%c) and %d (%c) are duplicate letters in your cipher.\n", cipherCurrent, Cipher[cipherCurrent], j, Cipher[j]);
  167.                            
  168.                             errorCount++;
  169.                         }
  170.                 }
  171.         }
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /* Check to ensure only lower case characters are used */
  178.    for (plaintextCurrent = 0; plaintextCurrent < plaintextLength; plaintextCurrent++)
  179.       {
  180.          if (isupper (plainText[plaintextCurrent])) /* Isupper command is used to do a check on the string and check for any uppercase letters */
  181.            {
  182.                printf("'%s' contains uppercase letters, please use lowercase only for the text.\n", plainText);
  183.                 errorCount++;
  184.            }
  185.       }
  186.  
  187.  
  188.    for (cipherCurrent = 0; cipherCurrent < cipherLength; cipherCurrent++)
  189.       {
  190.          if (isupper (Cipher[cipherCurrent]))
  191.            {
  192.                printf("'%s' contains uppercase letters, please use lowercase only for the cipher.\n", Cipher);
  193.                errorCount++;
  194.            }
  195.       }
  196.  
  197.  
  198.  
  199. /* -------------------------------------------------------- Pre-letter swap -------------------------------------------------------- */
  200.  
  201.  
  202. /* Could also use strlen, if(turnCounter = strlen(Cipher - 1)) with a for loop, less clean though */
  203.  
  204.  
  205.  
  206.   strncat(Cipher, Cipher, 1); /* This command will copy the first letter to the end of the Cipher to allow it to be used in the next stage */
  207.  
  208.  
  209.  
  210. /* -------------------------------------------------------- Letter swapping -------------------------------------------------------- */
  211.  
  212.     if(errorCount == 0) /* Will only activate if no errors have been flagged */
  213.         {
  214.             for(cipherCurrent = 0; cipherCurrent < cipherLength; cipherCurrent++)  /* This loops the cipherCurrent variable, which is used to check each letter of the cipher array */
  215.                {
  216.                 turnCounter++;   /* This is used to increase the step counter by one each time the Cipher step loops by one */
  217.        
  218.                     for(plaintextCurrent = 0; plaintextCurrent < plaintextLength; plaintextCurrent++)  /* This is used to loop the plaintext, this will loop through the entire array and check for letters that match the cipher  */
  219.                          {
  220.                             if(plainText[plaintextCurrent] == Cipher[cipherCurrent]) /* Performs the check for encryption, compares the letter on the text to the letter on the cipher */
  221.                                 {
  222.                                     encryptedText[plaintextCurrent] = Cipher[cipherCurrent + 1]; /* When it finds a match between letters in the plainText and Cipher Key, this line modifies the encryptedText variable as appropriate */
  223.                                 }
  224.                         }                 
  225.                    printf("Step %d replace %c with %c \n", turnCounter, Cipher[cipherCurrent], Cipher[cipherCurrent + 1]);
  226.                    printf("%s \n", encryptedText);   
  227.               }
  228.    
  229.         }
  230.        
  231.     else
  232.         {
  233.           printf("\nPlease correct these issues and try again.");
  234.         }
  235. }