<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. START
  3.  
  4. READ the first line of the INPUT; length of the cipher text
  5. STORE the length of the cipher text
  6. READ the second line of the INPUT; the cipher text
  7. STORE the 2-16 characters long the cipher text
  8. READ the third line of the INPUT; length of the plain text
  9. STORE the length of the plain text
  10. READ the fourth line of the input; the plain text
  11. STORE the 4-256 characters long plain text
  12.  
  13. LET output EQUALS plain text
  14.  
  15. FOR each character in cipher text
  16. WRITE number of steps
  17. THEN substitute current character of the cipher text with adjacent current character of the plain text
  18.  
  19. FOR each character in the plain text
  20. IF current character of the plain text EQUALS current character of the cipher text;
  21. THEN replace character of same index of output with character that is adjacent to the current character in the cipher text
  22.  
  23. END IF   
  24.  
  25. END FOR
  26.    
  27. PRINT the output (steps and final encrypted text)
  28.  
  29. END FOR
  30.  
  31. END
  32. */
  33.  
  34. #include <stdio.h>
  35.  
  36. int main()
  37. {
  38.  
  39. int key_length;
  40. int text_length;
  41. int cipher_key;
  42. int plain_text;//the four integers are initiliazed to be used in the program, the first two integers for storing key and text sizes and last two for the for loops
  43.  
  44. char key[16], text[256], output[256];// for creating strings by declaring character and setting their array limit
  45.  
  46. if(scanf("%d%*c", &key_length)!= 1)// input the length of cipher key to be used in integer form and c* which ignores any spaces or special characters, scanf function will check if input entered is true as 1 or false as 0, if value entered is not true then the code in the if statement will execute.
  47. {
  48. fprintf(stderr,"incorrect cipher key size, must be integer form\n");//after checking if the value entered is correct, which means in integer form, the scanf function should return value of 1 as true and 0 as false. The use of fprintf (file print function) is so that stderr (standard error) can be used.
  49. exit(1);//this indicates a failure; so the program will stop if the code in the if statement executes and then prints the message, because the value entered is not in integer form.
  50. }
  51.  
  52. scanf("%[^\n%*c]", key);// input the cipher key characters from the first parameter passed, \n is an escape sequence used here so that the input is consistent even if a newline is used in the input.
  53.  
  54. if (scanf("%d%*c", &text_length)!= 1)//input the length of plain text to be used in integer form and %c* which ignores any spaces or special characters and the previous character added by the user, scanf function will check if input entered is true as 1 or false as 0, if value entered is not true then the code in the if statement will execute.
  55. {
  56. fprintf(stderr,"incorrect plaintext size, must be integer form\n");//after checking if the value entered is correct, which means in integer form, the scanf function should return value of 1 as true and 0 as false. The use of fprintf (file print function) is so that stderr (standard error) can be used.
  57. exit(1);//this indicates a failure; so the program will stop if the code in the if statement executes and then prints the message, because the value entered is not in integer form.
  58. }
  59.  
  60. scanf("%[^\n%*c]", text);// input the length of plain text from the second parameter passed, \n is an escape sequence used so that the input is consistent even if a newline is used in the input, %c* so that any previous character added by the user is ignored.
  61.  
  62. strcpy(output, text);// the sctrcpy function here will handle two parameters; it will copy the contents of the text array into the output.
  63.  
  64. key[key_length]= key [0];// this is so that the last variable in the cipher array is set to the first variable in the cipher array.
  65.  
  66. for(cipher_key = 0; cipher_key < key_length; cipher_key++);// this is for looping through the cipher key size, the starting value of the loop is defined, and the condition of the loop through, and then adding to the value of the out loop.
  67. {
  68. printf("Step %d, substitute %c with %c\n", cipher_key+1, key[1], key[cipher_key]+1);//this is so that the user is informed about when the present and next values, within the cipher key, are interchanged.
  69.  
  70. for(plain_text = 0; plain_text < text_length; plain_text++);// this is a nested loop and is similar to the previous loop for the cipher, as here it creates a loop up until the size of the value given for the plain text.
  71. {
  72. if(text[plain_text] == key[cipher_key]);// this is a conditional statement which compares compares the text from the text loop with the key from the cipher loop, so it is testing whether they match.
  73. {
  74. output[plain_text] = key[cipher_key+1];// so this code will run if the condition met above is true, so that would be the text matching the key (as in letters), if ran this means the current text is replaced with the next letter available cipher key.
  75.  
  76. }
  77. }
  78. printf("%s\n, output");// by the end of the program this code will print out, in string, the final output of the text.
  79. return 0;
  80. }
  81. }