<mosaic.cnfolio.com>
PROGRAM
  1. /* START
  2.    >>>----------------------------- NUMBER OF SUBSTITUE-------------------------------<<<
  3.  
  4.    Obtain the number the characters that would be the length of the cipher key
  5.    Display the cipher key length
  6.    IF the length of cipher key is less than 2
  7.       THEN
  8.          Display error message for too little amount of characters
  9.       ELSE
  10.          IF the length of cipher key is more than 16
  11.             THEN
  12.                Display error message for too many characters
  13.          END IF
  14.    END IF
  15.    
  16.    >>>---------------THE CHARACTER THAT WILL BE USED FOR THE SUBSTITUTION-----------------<<<
  17.    
  18.    WHILE the number of loop is less than the length of the cipher key
  19.       Obtain the characters for the cipher key
  20.       IF the obtained cipher key characters were uppercase characters
  21.          THEN
  22.             Display error message that only lowercase characters are allowed
  23.          ELSE
  24.             IF the obtained cipher key characters were numbers
  25.                THEN
  26.                   Display error message that only lowercase character are allowed
  27.                ELSE
  28.                   IF the characters for the cipher key is less than the cipher key length
  29.                      THEN
  30.                         Display error message that cipher key and cipher key length must be the same
  31.             END IF
  32.       END IF
  33.    increase loop by one
  34.    END WHILE
  35.    
  36.    >>>----------------------------CHECKING FOR DUPLICATES---------------------------------<<<
  37.    
  38.    WHILE the number of loops is less than the length of the cipher key (variable X=0)
  39.        WHILE the number of loops is less than the length of the cipher key (variable Z= X+1)
  40.            IF the inputed cipher key character is the same as another cipher key character
  41.               THEN
  42.                  Display error message for having duplicates
  43.            END IF
  44.        increase loop by one
  45.        END WHILE
  46.    increase loops by one   
  47.    END WHILE
  48.    WHILE the inputed characters is not either a new line or an end of file, display all inputed characters
  49.    END WHILE
  50.    
  51.    >>>-----------------NUMBER OF CHARACTERS IN THE ORIGINAL TEXT--------------------------<<<
  52.    
  53.    Obtain the length of the text, including spaces
  54.    Display the text length
  55.    IF the text length is less than 4
  56.       THEN
  57.          Display error message for too little amount of characters
  58.       ELSE
  59.          IF the text length is more than 256
  60.             THEN
  61.                Display error message for too many characters
  62.          END IF
  63.    END IF
  64.    WHILE the inputed characters is not either a new line or an end of file, display all inputed characters
  65.    END WHILE
  66.    
  67.    >>>--------------------THE WORD THAT WOULD BE USED FOR THE SUBSTITUTION------------------<<<
  68.    
  69.    WHILE the loop is less than the length of the text
  70.        Obtain the plain text that would be used for the substitution, including spaces
  71.        IF the plain text included uppercase character
  72.           THEN
  73.              Display error message and that only lowercase characters are allowed
  74.           ELSE
  75.              IF the plain text included number
  76.                 THEN
  77.                    Display error message that number are not allowed, only lowercase characters
  78.                 ELSE
  79.                    IF the plain text is less than the text length
  80.                       THEN
  81.                          Display error message that plain text and text length must be the same
  82.                    END IF 
  83.              END IF
  84.        END IF
  85.    increase loop by one
  86.    END WHILE
  87.    Display the plain text
  88.    
  89.       >>>--------------------------STEP BY STEP SUBSTITUTION---------------------------------<<<
  90.    
  91.    initialise StepN to 1   
  92.    WHIlE the number of loop is less than the length of the text
  93.        WHILE the number of loop is less than the length of the cipher key
  94.            IF the plain text equals to any of the cipher key
  95.               THEN
  96.                  Replace that cipher key within the plain text with the next cipher key. If that cipher key was last,then replace with the first cipher key
  97.                  Display the step number, followed by the target character with the substitution character and the plain text after the substitution                 
  98.            ELSE
  99.               IF the the plain text does not equal to any of the cipher key
  100.                  THEN
  101.                     Display the step number, followed by the target character with the substitution character and the plain text that was outputted the line before
  102.               END IF
  103.            END IF
  104.        END WHILE
  105.    increase loop by one
  106.    END WHILE
  107. END
  108.                
  109. */
  110.    
  111. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include <string.h>
  114.  
  115. int main(void) {
  116.  
  117. //--------------------------------- NUMBER OF SUBSTITUE---------------------------------//
  118.  
  119.     int CipherKL;
  120.     int i, c;
  121.  
  122.     scanf(" %d", &CipherKL); // Inputting the number of character that would be used for the substitution //
  123.     if (CipherKL < 2) {// Values entered must be between 2 & 16 or error message will appear// 
  124.         printf("ERROR, length of cipher key must be bigger than 1\n");
  125.         return 0;
  126.     }else if (CipherKL > 16) {
  127.         printf("ERROR, length of cipher key must be less than 17\n");
  128.         return 0;
  129.     }     
  130.        
  131. //----------------------THE CHARACTER THAT WILL BE USED FOR THE SUBSTITUTION---------------------//
  132.  
  133.     char CipherLetters[16];
  134.     for (i=0; i<CipherKL; i++) {       
  135.         scanf(" %c", &CipherLetters[i]);// inputting the characters that would be substituted //
  136.         if( CipherLetters[i] >= 'A' && CipherLetters[i] <= 'Z'){// If a upper case character was inputted, error message will appear //
  137.            printf("ERROR, letters must be lower case");
  138.            return 0;
  139.         }else if( (CipherLetters[i] >= '0' ) && (CipherLetters[i] <= '9' ) ){// If a number was inputted, error message will appear //
  140.            printf("ERROR, you have inputed an integer, please enter a lower case letter");
  141.            return 0;
  142.         }else if( CipherLetters[i] < CipherKL){
  143.            printf("ERROR, Text cipher key and cipher key length must be the same\n");
  144.            return 0;
  145.         }  // program ignores any extra characters, meaning that they would not be substituted//
  146.     }
  147.    
  148. //-------------------------------------CHECKING FOR DUPLICATES----------------------------------//
  149.  
  150.     int j;
  151.     for (i=0; i<CipherKL; i++) { // Essentialy this is very similar to the bubble sort algorithm //
  152.         for (j=i+1; j<CipherKL; j++) { // j is always infront of i, therefore, allowing them to compare each input checking if there are any duplicates //
  153.             if (CipherLetters[i] == CipherLetters[j]) {//If the condition is met, an error message will appear //
  154.                 printf("ERROR, you have repeated an input, please only enter a character once");
  155.                 return 0;
  156.             }
  157.         }
  158.     }   
  159.     while ((c = getchar()) && c != '\n' && c!= EOF);
  160.     // This reads all inputs one at a time, but when it gets to a new line or to the end of file it would stop //
  161. // Essentially this ignores any extra character, e.g saying that there will be 11 characters but inputs 12, program would ingore the 12th character //
  162.  
  163. //----------------------NUMBER OF CHARACTERS IN THE ORIGINAL TEXT----------------------//
  164.  
  165.     int TextL;
  166.     scanf(" %d", &TextL);   // This ask for the number of characters that the word will have, including spaces //
  167.      
  168.     if (TextL < 4) {// Values entered must be between 4 & 256 or error message will appear //
  169.        printf("ERROR, length of plain text including spaces must be more than 3 characters\n");
  170.        return 0;
  171.     }else if (TextL > 256) {
  172.        printf("ERROR, length of plain text including spaces must be less than 257 characters\n");
  173.        return 0;
  174.     }
  175.  
  176.     while ((c = getchar()) && c != '\n' && c!= EOF);
  177.          
  178. //----------------------THE WORD THAT WOULD BE USED FOR THE SUBSTITUTION----------------------//
  179.  
  180.     char SubWord[256];
  181.     for (j=0; j<TextL; j++) {
  182.         scanf("%[^\n]s", SubWord);// The word that would be used for the substitution //
  183.         if(SubWord[j] >= 'A' && SubWord[j] <= 'Z'){// If a upper case character was inputted, error message will appear //
  184.            printf("ERROR, letters must be lower case");
  185.            return 0;
  186.         }else if((SubWord[j] >= '0' ) && (SubWord[j] <= '9') ){// If a number was inputted, error message will appear //
  187.            printf("ERROR,  please only enter a lower case letter");
  188.            return 0;
  189.         }  else if( SubWord[j] < TextL){
  190.            printf("Error, Text length must be the same as the number of characters within the plain text\n");
  191.            return 0;
  192.         }  // program ignores any extra characters, meaning that they would not be substituted//
  193.     }
  194.        
  195. //------------------------------------STEP BY STEP SUBSTITUTION------------------------------------//
  196.  
  197.    int StepN = 1; // Initialising that each step will start with, Step 1 //   
  198.    
  199.        for (i=0; i<TextL; i++) { // For each character in the sub word
  200.             for (j=0; j<CipherKL; j++) { // For each letter in the cipher key 
  201.            
  202.                  if(SubWord[i] == CipherLetters[j]) { // If the subword matches the cipher characters, sub that letter with the next letter//
  203.                     SubWord[i] = CipherLetters[(j+1)%CipherKL];
  204.                     printf("Step %d: Sub %c with %c\n", StepN++, CipherLetters[j], CipherLetters[(j+1)%CipherKL]); // Everytime it completes a loop, the value will increment by 1 //
  205.                     printf("%s\n", SubWord);
  206.                     break;
  207.                    
  208.              /*  }else if(SubWord[i] != CipherLetters[j]) {
  209.                     SubWord[i] = CipherLetters[j];
  210.                     printf("Step %d: Sub %c with %c\n", StepN++, CipherLetters[j], CipherLetters[(j+1)%CipherKL]); // Everytime it completes a loop, the value will increment by 1 //
  211.                     printf("%s\n", SubWord);
  212.                     break;
  213.                  */
  214.            
  215.                 } // Subsistution order: Whatever letter is subbed in, the next target value would be the letter after it not the same as the subbed in letter//
  216.             }
  217.         }
  218. }