<mosaic.cnfolio.com>
PROGRAM
  1. //---------------------------------------- Pseudo Code -------------------------------------------//
  2.  
  3. /*
  4. START
  5.  
  6.    Insert the number for length of the cipher key. (form 2 to 16)
  7.    
  8.    IF there are any mistakes.
  9.       THEN Print what is the mistake.
  10.     ELSE Insert the cipher key text (any 2 to 16 characters, with out any dublicates and only lower case characters(a-z)).
  11.             
  12.       IF there are any mistakes.
  13.        THEN Print what is the mistake.
  14.          ELSE Insert the number for length of the plain text. (form 4 to 256).
  15.                
  16.          IF there are any mistakes.
  17.             THEN Print what is the mistake.
  18.             ELSE Insert the plain text (any 4 to 256 characters, only lower case characters(a-z)).
  19.                         
  20.             IF there are any mistakes.
  21.                THEN Print what is the mistake.
  22.                ELSE Do the encryption.
  23.                               
  24.                   count = 0.
  25.                   WHILE count < number for length of the cipher key +1.
  26.                    Check by one all characters that are in the cipher key.
  27.                        
  28.                    IF this character appears in the plain text.
  29.                        Substitute all same characters in the plain text to the characters that follow next in the cipher key next(if character was at last position in the cipher key, use the character at the first position in the cipher key).
  30.                         Print which of two characters of cipher key did you substitute with each other and inserted in the plain text.
  31.                      END IF
  32.                             
  33.                      Print the result of encrypted plain text.
  34.                      count + 1.
  35.                   END WHILE
  36.                      
  37.              END IF
  38.                
  39.           END IF
  40.            
  41.        END IF
  42.          
  43.     END IF
  44.            
  45. END
  46. */
  47.  
  48.  
  49. //----------------------------------------- Code Itself --------------------------------------------//
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. // Definition of the size for а cipher key
  55. #define MAX_NUMBER_FOR_CIPHERKEY       16
  56. #define MIN_NUMBER_FOR_CIPHERKEY       2
  57.  
  58. // Definition of the size for а plain text
  59. #define MAX_NUMBER_FOR_PLAINTEXT       256
  60. #define MIN_NUMBER_FOR_PLAINTEXT       4
  61.  
  62. //KEYBO CHARACTERS  (From ASCII Table we know that each letter has it own certain number. Numbers from 97 to 122 represent letters (a-z). Look it up yourself: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html )
  63. #define IN_RANGE_FROM       97
  64. #define IN_RANGE_TO         122
  65.  
  66.  
  67. //----------------------------------------- Main Part --------------------------------------------//
  68.  
  69. int main (void){
  70.  
  71.   char cipherInp[MAX_NUMBER_FOR_CIPHERKEY],        // cipherInp - а storage, to hold input for a cipher key.
  72.   plnText[MAX_NUMBER_FOR_PLAINTEXT],                  // plnText   - а storage, to hold input for a plain text.
  73.   plnText2[MAX_NUMBER_FOR_PLAINTEXT];            // plnText2  - а duplicate for plnText, to keep original input if plnText undergoes any changes.
  74.  
  75.   int ncip,    // ncip - a number, to indicate a size of a cipher key.
  76.   npl,               // npl  - a number, to indicate a size of a plain text.
  77.   i, j, l, m,      // i,j,l,m - empty spaces, to store a certain numbers.
  78.   Error = 0;       // Error - a checklist, to make a certain sign if an error exists.
  79.  
  80.  
  81. //-------------------------- Responsible Part Of Reading The Cipher Key ---------------------------//
  82.  
  83.   scanf("%d ", &ncip);
  84.  
  85.   if ( ncip >= MIN_NUMBER_FOR_CIPHERKEY && ncip <= MAX_NUMBER_FOR_CIPHERKEY){           // First of all, we want to check if a number is in the admissible range.
  86.  
  87.     for(i = 0; i < ncip; i++)                    
  88.     {scanf("%c", &cipherInp[i]);}
  89.    
  90.     for(i=0; i < ncip; i++){
  91.    
  92.       if((cipherInp[i] < IN_RANGE_FROM || cipherInp[i] > IN_RANGE_TO) && Error == 0)    // Here, we want to check if the size of a word is matching the length of a number.
  93.       {Error = 2;}
  94.      
  95.       for(j=i+1;j < ncip+1;j++){                                                        // Searching for any duplicates in input of the cipher key.
  96.        
  97.         if((cipherInp[i]==cipherInp[j]) && Error == 0)
  98.         {Error = 1;}
  99.        
  100.       }
  101.     }
  102.   }
  103.  
  104.   else{Error = 3;}                                                // Indicates the occurrence of an error - a failure. (P.s. - Each failure has its own number.)
  105.  
  106.  
  107. //-------------------------- Responsible Part Of Reading The Plain Text ---------------------------//
  108.  
  109.   scanf("%d ", &npl);
  110.  
  111.   if ( npl >= MIN_NUMBER_FOR_PLAINTEXT && npl <= MAX_NUMBER_FOR_PLAINTEXT){                  // As in the cipher key, we want to check if a number is in the admissible range.
  112.  
  113.     for(j = 0; j < npl; j++){
  114.       scanf("%c", &plnText[j]);
  115.       plnText2[j] = plnText[j];                                          // At this point, we want to remember what was the original input for the plain text.
  116.      
  117.       if ((plnText[j] < IN_RANGE_FROM || plnText[j] > IN_RANGE_TO) && plnText[j] != 32)     // Similar to the cipher key question but with including a space now.
  118.       {Error = 4;}
  119.      
  120.     }
  121.   }
  122.  
  123.   else{Error = 5;}                                                   // Notice: even if we have a similar question but only because we work with a plain text now. Failure has a different number now.
  124.  
  125.  
  126. //---------------------------------------- Final Output ------------------------------------------//
  127.  
  128.   if(Error == 0){                          // If the code passed through the entire script and did not find a single failure (Error still equals to 0), then it's will run last bit of the scrip. Otherwise, it will occur amendment fee - skip this part and move to Encyclopedia Of Errors.
  129.     for(i = 0; i < ncip; i++){
  130.       if (i == ncip - 1)
  131.       {l = i-i;}
  132.        
  133.       else {l = i+1;}
  134.      
  135.       printf("\nStep %d substitute %c with %c\n", i+1, cipherInp[i], cipherInp[l]);    // Shows which letters have been changed in the text to others
  136.      
  137.       for( int j = 0; j <= npl; j++){                                    // This part allows you to make changes to the plnText2 without affecting already existing changes in the plnText.
  138.         if(plnText[j] == cipherInp[i]){
  139.           plnText2[j] = cipherInp[l];
  140.          
  141.           if (i == ncip - 1)                                          // Sets back to the first letter.
  142.           {plnText2[j] = cipherInp[l];}
  143.          
  144.         }
  145.       }
  146.      
  147.       for( int m=0; m < npl; m++)                                                        // The main reason for which we needed a duplicate.
  148.       {printf("%c", plnText2[m]);}                                      // Since we synchronized step by step every change letter from the original inто a duplicate,
  149.                                                                // The duplicate no longer synchronized the changes referring to changes the first steps in the original.
  150.     }
  151.   }
  152.  
  153.  
  154. //------------------------------------ Encyclopedia Of Errors ------------------------------------//
  155.  
  156.  
  157.   // Simply having read the answer which will be written in case of this or that malfunction,
  158.   // You should be able to understand what that speech is about and that, for each of these failures, bears in itself the code - script.
  159.  
  160.  
  161.   if(Error == 1){
  162.     printf("\nError #1\nSorry, you can't have any duplicates elements in the cipher key.\nYour duplicate element/s is/are: ");
  163.    
  164.     for(i=0; i < ncip; i++){                   
  165.       for(j=i+1;j < ncip;j++){
  166.         if(cipherInp[i]==cipherInp[j])
  167.         {printf("%c ",cipherInp[i]);}
  168.        
  169.       }
  170.     }
  171.   }
  172.  
  173.  
  174.  
  175.   else if(Error == 2){
  176.     for(i=0; i < ncip; i++){
  177.       if(cipherInp[i] < IN_RANGE_FROM || cipherInp[i] > IN_RANGE_TO){
  178.           printf("\nError #2\nSorry, you can insert only lower case characters(a-z) in the cipher key.\nYou have inserted: %c", cipherInp[i]);
  179.       }
  180.     }
  181.   }
  182.  
  183.  
  184.  
  185.   else if(Error == 3){
  186.     printf("\nError #3\nSorry, you can only insert numbers form 2 to 16 in the cipher key.\nYou have inserted: %d.", ncip);
  187.   }
  188.  
  189.  
  190.  
  191.   else if(Error == 4){
  192.     for(j = 0; j < npl; j++){
  193.       if ((plnText[j] < IN_RANGE_FROM || plnText[j] > IN_RANGE_TO) && plnText[j] != 32){
  194.         printf("\nError #4\nSorry, you can insert only lower case characters(a-z) and SPACE in the plain text.\nYou have inserted: %c", plnText[j]);
  195.       }
  196.     }
  197.   }
  198.  
  199.  
  200.  
  201.   else if(Error == 5){
  202.     printf("\nError #5\nSorry, you can only insert numbers form 4 to 256 in the plain text.\nYou have inserted: %d", npl);
  203.   }
  204.    
  205.    
  206.   return (0);
  207. }
  208.  
  209.  
  210. //------------------------------------------THE END---------------------------------------------//