<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. GET cipher length
  3. IF cipher length is NOT betweem 2 to 16
  4.    THEN
  5.       Enable ERROR
  6.       DISPLAY reason of error
  7. END IF
  8. GET cipher text
  9. IF number of characters in cipher text is NOT between 2 to 16
  10.    THEN
  11.       ENABLE ERROR
  12.       DISPLAY reason of error
  13. END IF
  14. IF the entered cipher text length does not match with the length of cipher text
  15.    THEN
  16.       ENABLE ERROR
  17.       DISPLAY reason of error
  18. END IF
  19. IF found capital letter in cipher text
  20.    THEN
  21.       Enable ERROR
  22. END IF
  23. IF found any duplicates in cipher text
  24.    THEN
  25.       Enable ERROR
  26. END IF
  27. IF found any character not between a-z in cipher text
  28.    THEN
  29.       Enable ERROR
  30. END IF
  31. GET plain text length
  32. IF plain text length is NOT between 4 to 256 characters
  33.    THEN
  34.       Enable ERROR
  35. END IF
  36. GET plain text
  37. IF plain text characters is NOT between 4 to 256 characters
  38.    THEN
  39.       Enable ERROR
  40. END IF
  41. IF found capital letter in plain text
  42.    THEN
  43.       Enable ERROR
  44. END IF
  45.  
  46. IF FLAG is Enabled
  47.    THEN
  48.       DISPLAY ERROR
  49. END IF
  50.  
  51. IF ERROR is Disabled
  52.    THEN
  53.       WHILE CURRENT POSITION has not gone past the last character of cipher text
  54.          IF CURRENT POSITION is on the last character of cipher text
  55.             THEN
  56.                TAKE the last character of the cipher text as the TARGET character
  57.                TAKE the first character of the cipher text as the SUBSTITUTION character
  58.          END IF
  59.          ELSE
  60.             TAKE the character in the CURRENT POSITION of the cipher text as the TARGET character
  61.             TAKE the character right to the CURRENT POSITION of the cipher text as the SUBSTITUTION character
  62.          END ELSE
  63.          IF TARGET character is found in the plain text
  64.             THEN
  65.                REPLACE TARGET character with SUBSTITUTION character in the plain text encoded
  66.                DISPLAY the step number and the TARGET character replaced by the SUBSTITUTION character
  67.               DISPLAY the encrypted plain text
  68.          END IF
  69.          INCREMENT CURRENT POSITION
  70.       END WHILE
  71. END IF
  72. */
  73.    
  74. #include <stdio.h>
  75. #include <string.h>
  76.  
  77. int test=0;
  78.  
  79. int cipher_length, plain_text_length;
  80. int error=0;
  81. char cipher_text[17], plain_text[257], plain_text_encoded[257]={'\0'};
  82. int counter=0;
  83. int i=0;
  84. int capital=0, duplicate=0, unwanted_character=0;      //error flags
  85. int position=0;
  86. int N=1; // current position of cipher key
  87. char target, subtitution;
  88. int step=1, array_cell=0;
  89.  
  90. int main(void) {
  91.    //Start of user input errors handling section
  92.    scanf("%d",&cipher_length);
  93.    if(cipher_length<2 || cipher_length>16){              //check if cipher length is NOT between 2 to 16
  94.       error=1;
  95.       printf("Error! The cipher length entered is not between 2 to 16.\n");
  96.    }
  97.  
  98.    scanf("%s",cipher_text);
  99.    if(strlen(cipher_text)!= cipher_length){              //checking if number of characters in cipher text is equal to cipher length
  100.       error=1;
  101.       printf("Error! The entered cipher length does not match with the length of cipher text.\n");
  102.    }
  103.    
  104.    if(strlen(cipher_text)<2 || strlen(cipher_text)>16){      //checking if number of characters in cipher text is NOT between 2 to 16
  105.       error=1;
  106.       printf("Error! The number of characters in the cipher text is not between 2 to 16.\n");
  107.    }
  108.    while (counter<=strlen(cipher_text)) {        //checking if there is a capital letter in cipher text
  109.       if (cipher_text[counter] >= 'A' && cipher_text[counter] <= 'Z'){
  110.          capital=1;                              //set capital flag to 1
  111.       }
  112.       counter++;                                //used to count through the characters in the cipher text
  113.    }
  114.    counter=0;                                  //set back to zero to be used later                                       
  115.    for(position=0; position<strlen(cipher_text); position++){
  116.         counter=position+1;
  117.         while(counter<=strlen(cipher_text)){
  118.             if(cipher_text[position]==cipher_text[counter]){
  119.                duplicate=1;                                    //set duplicate flag to 1
  120.             }
  121.             counter++;
  122.         }
  123.    }
  124.    counter=0;                                  //set back to zero to be used later
  125.    while (counter<strlen(cipher_text)) {                //checking if there is any duplicate letter
  126.       if((cipher_text[counter]>='a' && cipher_text[counter]<='z')){
  127.          unwanted_character=0;
  128.       }
  129.       else{
  130.          unwanted_character=1;                                 //set unwanted character flag to 1
  131.       }
  132.       counter++;
  133.    }
  134.    counter=0;                                  //set back to zero to be used later
  135.    
  136.    scanf("%d", &plain_text_length);          //get the plain text length
  137.    if(plain_text_length<4 || plain_text_length>256){
  138.       error=1;
  139.       printf("Error! Plain text length must be between 4 to 256.\n");
  140.    }
  141.    scanf(" %[^\n]s",plain_text);
  142.    if(plain_text_length != strlen(plain_text)){      //if the number of characters in plain text is not equal to the length of plain text
  143.       error=1
  144.       printf("Error! The entered plain text length does not match with the length of plain text");
  145.    }
  146.       while (counter<=strlen(plain_text)) {                //checking if there is a capital letter in plain text
  147.       if (plain_text[counter] >= 'A' && plain_text[counter] <= 'Z'){
  148.          capital=1;                              //set capital flag to 1
  149.       }
  150.       counter++;                                //used to count through the characters in the cipher text
  151.    }
  152.    counter=0;                                  //set back to zero in case used later
  153.    if(capital!=0){                                             //checking if capital flag is set
  154.       printf("Error! No capital letter is allowed in Cipher text and Plain text.\n");
  155.       error=1;
  156.    }
  157.    if(duplicate!=0){               //checking if duplicate flag is set
  158.       printf("Error! No duplicate character is allowed in cipher text.\n");
  159.       error=1;
  160.    }
  161.    if(unwanted_character!=0){            //checking if unwanted character flag is set
  162.       printf("Error! only lower case letters between a to z is allowed.\n");
  163.    }
  164.    //End of user input errors handling section
  165.    if(error==0){
  166.       strcpy(plain_text_encoded,plain_text);              //copy plain text array into plain text encoded array
  167.       while(N <= cipher_length){           //go through every character until current position reaches the last character in cipher key
  168.          if(N==cipher_length){                                 //check of the position of the cipher text is on the last character
  169.             target = cipher_text[cipher_length-1];     //target is the cell of the cipher text array
  170.             subtitution = cipher_text[0];       //substituion is the first cell of the cipher text
  171.          }
  172.          else{
  173.             target = cipher_text[N-1];
  174.             subtitution = cipher_text[N];
  175.          }
  176.          for(i=0; i<=plain_text_length; i++){                  //go through every character in plain text array
  177.             if(plain_text[i]==target){         //find target character in the plain text
  178.                plain_text_encoded[i] = subtitution;            //replace target character with subtitution character
  179.             }
  180.          }
  181.             printf("Step %d substitute %c with %c\n",step,target,subtitution);
  182.             printf("%s\n",plain_text_encoded);
  183.             step++;
  184.             N++; 
  185.       }
  186.    }
  187. }