<mosaic.cnfolio.com>
PROGRAM
  1. /*length of cypher key is inputted
  2.  
  3. WHILE length of cypher key is less than 2
  4.    Display "ERROR: cypher key must be greater than 1"
  5.    length of cypher key is re-inputted until the user inputs a value that is equal or greater than 2
  6. END WHILE
  7.  
  8. WHILE length of cypher key is greater than 16
  9.    Display "ERROR: cypher key must be less than 17"
  10.    length of cypher key is re-inputted until the user inputs a value that is equal or less than 16
  11. END WHILE
  12.  
  13. a character for the cypher key is inputted
  14.  
  15. WHILE the amount of characters inputted is less than cypher key length
  16.    input another character
  17.    WHILE the character inputted is not lower case
  18.       Display "ERROR:the letter that has just been used must be in lower case"
  19.       the character is corrected and the rest of the cypher key is inputted
  20.    END WHILE
  21. END WHILE
  22.  
  23.  
  24. Length of normal text is inputted
  25.  
  26. WHILE length of normal text is less than 4
  27.    Display "ERROR: Normal Text must be greater than 3"
  28.    length of main text is re-inputted until the user inputs a value that is equal or greater than 4
  29. END WHILE
  30.  
  31. WHILE length of normal text is greater than 256
  32.    Display "ERROR: normal text must be less than 257"
  33.    length of main text is re-inputted until the user inputs a value that is equal or less than 256
  34. END WHILE
  35.  
  36. a character for the normal text is inputted
  37.  
  38. WHILE the amount of characters inputted is less than normal text length
  39.    input another character until the number of characters
  40.    WHILE the character inputted is not lower case
  41.       Display "ERROR:the letter that has just been used must be in lower case"
  42.       the character is corrected and the rest of the normal text is inputted
  43.    END WHILE
  44. END WHILE
  45.  
  46. WHILE more letters need to be substituted
  47. display increment the number of steps, the target letter to be substituted and the letter to substitute with
  48. display the corrected normal text with the new substitutions
  49. END WHILE
  50. */
  51. #include <stdio.h>
  52. #include <string.h>
  53. int main(void){
  54. int clength, mlength;         /*integers to find cypher length and main text length*/
  55.  
  56. scanf("%d",&clength);         /*scan function to allow input of cypher length with use of %d to declare as integer*/
  57.    
  58. if(clength<= 1){          /*if function so that the cypher length is greater than 1*/
  59.    printf("ERROR:cypher length must be greater than 1");  /*prints the relevant error message*/
  60.    exit(1);       /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  61. }
  62.  
  63. if(clength>= 17){     /*if function so that the cypher length is less than 17*/
  64.    printf("ERROR:cypher length must be less than 17");   /*prints the relevant error message*/
  65.    exit(1);       /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  66. }
  67.  
  68. char cypher[18];          /*sets the string limit*/
  69. int i=0;        /*sets i for incrimant to 0 */
  70.  
  71. for(i=0;i<=clength; i++){    /*the loop that scans each individual input into the cypher string to the limit of the indicated clength*/
  72.     scanf("%c", &cypher[i])/*scans the character inputted and adds it to the string*/
  73.     if(isupper(cypher[i])){   /*if function to see if the letter inputted is uppercase*/
  74.       printf("\nERROR:the letter that has just been used in the cypher must be in lower case");   /*prints relevant error message if true*/
  75.       exit(1);      /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  76.     }
  77. }
  78.  
  79. scanf("%d", &mlength);      /*scan function to allow input of main text length with use of %d to declare as integer*/
  80.  
  81. if(mlength<=3){               /*if function so that the main text length is greater than 3*/
  82.    printf("\nERROR: normal text length must be greater than 3")/*prints the relevant error message*/
  83.    exit(1);       /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  84. }
  85.  
  86. if(mlength>=257){     /*if function so that the main text length is less than 257*/
  87.    printf("\nERROR: normal text length must be less than 257");   /*prints the relevant error message*/
  88.    exit(1);       /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  89. }
  90.  
  91. char mtext[257];          /*sets the string limit*/   
  92.  
  93. for(i=0;i<=mlength; i++){    /*the loop that scans each individual input into the cypher string to the limit of the indicated mlength*/
  94.     scanf("%c", &mtext[i]);   /*scans the character inputted and adds it to the string*/
  95.     if(isupper(mtext[i])){  /*if function to see if the letter inputted is uppercase*/
  96.       printf("\nERROR:the letter that has just been used in the main text must be in lower case");   /*prints relevant error message if true*/
  97.       exit(1);}                  /*exits the programme prematurely in order to ensure that the user can correct his mistake*/
  98.       if(!isalpha(mtext[i])&&!isspace(mtext[i])){    /*if function to see if it is a character or punctuation*/
  99.       printf("\n ERROR: No punctuation can be used in the main text")/*prints relevant error message if true*/
  100.       exit(1)
  101.     }
  102. }
  103. int j=0;        /*sets variable to be used in loop*/
  104. for(j=1;j<=clength; j++){    /*for loop to cycle through the cypher*/
  105.    if(cypher[j]==cypher[clength]){    /*if function to loop the last cypher character to the first cypher character in last step*/
  106.    printf("\nStep %d substitute %c with %c\n", (clength), cypher[j], cypher[1]); /*loop the last cypher character to the first cypher character in last step*/
  107.    }
  108.    if(cypher[j]!=cypher[clength]){    /*if the step is not the last step then this is used*/
  109.    printf("\nStep %d substitute %c with %c\n", j, cypher[j], cypher[j+1]); /*displays the step and shows what letter in the main text is being converted to the next in the cypher*/
  110.    }
  111.    for(i=1; i<=mlength; i++){    /*for loop to cycle through the main text so for every cypher character it checks all the main text*/
  112.       if(cypher[j]==mtext[i]){         /*if the main text character is the same as the selected cypher character it activates*/
  113.          if(cypher[j]==cypher[clength]){ /*if on the last step this activates and loops to the first cypher character*/
  114.             mtext[i]=cypher[1];}     /*the main text character equals the first cypher character*/
  115.          if(cypher[j]!=cypher[clength]){ /*if it is not on the last step this function activates*/
  116.             mtext[i]=cypher[j+1];}      /*this changes the main text character to the next on the cypher string*/
  117.       }
  118.    printf("%c", mtext[i]);       /*displays the corrected main text with the changes from the cypher*/
  119.     }
  120.  
  121. }
  122. }