<mosaic.cnfolio.com>
PROGRAM
  1. /******************************************************************************
  2.  
  3. PSEUDO CODE
  4.  
  5. Helper functions
  6. BEGIN checkIfAllLower
  7.     FOR all chars in a string
  8.         IF not lower case
  9.             RETURN false
  10.         ENDIF
  11.     ENDFOR
  12.     return true
  13. END checkIfAllLower
  14.  
  15. BEGIN containsSpaces
  16.     FOR all chars in a string
  17.         IF char is a space
  18.             RETURN true
  19.         ENDIF
  20.     ENDFOR
  21.     return false
  22. END containsSpaces
  23.  
  24. BEGIN containsDuplicates
  25.     FOR all chars in a string
  26.         FOR all chars in a string
  27.             IF outer char equals inner char
  28.                 return true
  29.             ENDIF
  30.         ENDFOR
  31.     ENDFOR
  32.     return false
  33. END containsDuplicates
  34.  
  35. BEGIN main
  36.     INPUT cipher length
  37.     IF cipher < 2 or > 16
  38.        OUTPUT Error: The Cipher's Length must be between 2-16 (inclusive).
  39.     ENDIF
  40.     INPUT cipher key text
  41.     IF cipher text length not equal to cipher length
  42.       OUTPUT Error: The Cipher Key must be the same length as the cipher length input.
  43.     ENDIF
  44.    
  45.     IF cipher text contains spaces
  46.         OUTPUT Error: The Cipher Key must contain no spaces.
  47.     ENDIF
  48.    
  49.     IF cipher text contains duplicates
  50.         OUTPUT Error: The Cipher Key not contain duplicates.
  51.     ENDIF
  52.    
  53.     IF cipher text contains all lower case letters
  54.         OUTPUT Error: The Cipher Key must be all lower case letters.
  55.     ENDIF
  56.     INPUT length of plain text string to cipher
  57.     IF length of plain text < 4 or > 256
  58.       OUTPUT Error: The Plain Text to encrypt must be between 4-256 characters long (inclusive).
  59.     ENDIF
  60.     INPUT plain text string to cipher
  61.      IF plain text string to cipher is not equal to length of plain text string to cipher
  62.         OUTPUT Error: Plain Text must be the same length as the Plain Text length input.
  63.     ENDIF
  64.    
  65.     IF plain text string to cipher is not all lowercase
  66.         OUTPUT Error: The Plain Text must be all lower case letters.
  67.     ENDIF
  68.  
  69.     make copy of plain text string to cipher
  70.    
  71.     FOR N length of cipher
  72.         get Nth charecter
  73.         WHILE Nth char is not NULL
  74.             IF N is equal to cipher length
  75.                 replace plain text char with first char in cipher key
  76.             ELSE
  77.                 replace plain text char with next cipher key char
  78.             ENDIF
  79.         ENDWHILE
  80.         IF N not equal cipher key length
  81.             OUTPUT Step N substitute Nth cipher key char with Nth cipher key char + 1
  82.         ELSE
  83.             OUTPUT Step N substitute Nth cipher key char with the first cipher key char
  84.         ENDIF
  85.         OUTPUT substituted plain text
  86.     ENDFOR
  87. END main
  88.  
  89. ******************************************************************************/
  90.  
  91. #include <stdio.h>
  92. #include <stdbool.h>
  93. #include <string.h>
  94.  
  95. bool checkIfAllLower(char string[]) {
  96.   for (int i = 0; i < strlen(string); i++){
  97.       if (!islower(string[i]) && string[i] != ' ') {
  98.           return false;
  99.       }
  100.   }
  101.   return true;
  102. }
  103.  
  104. bool containsSpaces(char string[]) {
  105.   for (int i = 0; i < strlen(string); i++){
  106.       if (string[i] == ' ') {
  107.           return true;
  108.       }
  109.   }
  110.   return false;
  111. }
  112.  
  113. bool containsDuplicates(char string[]) {
  114.   for (int i = 0; i < strlen(string); i++) {
  115.       for (int j = i + 1; j < strlen(string); j++) {
  116.           if (string[i] == string[j]) {
  117.               return true;
  118.           }
  119.       }
  120.   }
  121.   return false;
  122. }
  123.  
  124. void main()
  125. {
  126.  
  127.   int cipherLength;
  128.   char cipher[16];
  129.   int textLength;
  130.   char stringToCipher[256];
  131.  
  132.   char newCipherText[256];
  133.  
  134.   bool error = false;
  135.   scanf("%d", &cipherLength);
  136.   if (cipherLength < 2 || cipherLength > 16) {
  137.       printf("Error: The Cipher's Length must be between 2-16 (inclusive).\n");
  138.       error = true;
  139.   }
  140.  
  141.   scanf(" %[^\n]s", &cipher);
  142.   if (strlen(cipher) != cipherLength) {
  143.       printf("Error: The Cipher Key must be the same length as the cipher length input.\n");
  144.       error = true;
  145.   }
  146.  
  147.   if (containsSpaces(cipher)) {
  148.       printf("Error: The Cipher Key must contain no spaces.\n");
  149.       error = true;
  150.   }
  151.  
  152.   if (containsDuplicates(cipher)) {
  153.       printf("Error: The Cipher Key not contain duplicates.\n");
  154.       error = true;
  155.   }
  156.  
  157.   if (!checkIfAllLower(cipher)) {
  158.       printf("Error: The Cipher Key must be all lower case.\n");
  159.       error = true;
  160.   }
  161.  
  162.   scanf("%d", &textLength);
  163.   if (textLength < 4 || textLength > 256) {
  164.       printf("Error: The Plain Text to encrypt must be between 4-256 characters long (inclusive).\n");
  165.       error = true;
  166.   }
  167.  
  168.   scanf(" %[^\n]s", &stringToCipher);
  169.   if (strlen(stringToCipher) != textLength) {
  170.       printf("Error: Plain Text must be the same length as the Plain Text length input.\n");
  171.       error = true;
  172.   }
  173.  
  174.   if (!checkIfAllLower(stringToCipher)) {
  175.       printf("Error: The Plain Text must be all lower case.");
  176.       error = true;
  177.   }
  178.  
  179.   if (error == true) {
  180.       return 0;
  181.   }
  182.  
  183.   strcpy(newCipherText, stringToCipher);
  184.      
  185.   for (int N = 1; N - 1 < cipherLength; N++) {
  186.       //Variable to store the position of any characters to be replaced
  187.       char * first;
  188.       first = strchr(stringToCipher, cipher[N - 1]);
  189.       while (first != NULL) {
  190.           //If on the last loop get cipher char from start else get the next char
  191.           if (N == cipherLength) {
  192.               //change the characters at the right position to the ciphers char
  193.               newCipherText[first - stringToCipher] = cipher[0];
  194.               first = strchr(first + 1, cipher[0]);
  195.           } else {
  196.               newCipherText[first-stringToCipher] = cipher[N];
  197.               first = strchr(first + 1, cipher[N]);
  198.           }
  199.       }
  200.       //If to print out 2 correct characters being replaced
  201.       if (N != cipherLength) {
  202.           printf("Step %d substitute %c with %c\n", N, cipher[N - 1], cipher[N]);
  203.       } else {
  204.           printf("Step %d substitute %c with %c\n", N, cipher[N - 1], cipher[0]);
  205.       }
  206.       printf("%s\n", newCipherText);
  207.   }
  208. }