<mosaic.cnfolio.com>
PROGRAM
  1. /*ENG 421 CW2*/
  2.  
  3. /*
  4. PSEUDO CODE
  5.  
  6. GET INPUT of the cipher key length
  7. GET INPUT of the cipher key text
  8. GET INPUT of the original text length
  9. GET INPUT of the original text
  10.  
  11. IF the length of the cipher key text is between 2 and 16 characters THEN
  12.    IF the length of the original text is between 4 and 256 characters THEN
  13.       IF all cipher key text characters are alphabetical and every character is unique THEN
  14.          IF the original text contains only alphabetical characters and spaces THEN
  15.             Input Validation has been passed
  16.          ELSE
  17.             PRINT error: some original text characters are invalid
  18.          END IF
  19.       ELSE
  20.          PRINT error: some cipher key characters are invalid, or there are duplicates
  21.       END IF
  22.    ELSE
  23.       PRINT error: original text has an invalid length
  24.    END IF
  25. ELSE
  26.    PRINT error: cipher text has an invalid length
  27. END IF
  28.  
  29.  
  30. IF input validation has been passed THEN
  31.    INITIALISE N=1
  32.    INITIALISE i=1
  33.    WHILE N is less than or equal to cipher key length THEN
  34.       i=1
  35.       PRINT current step details
  36.       WHILE i is less than or equal to text length THEN
  37.          IF text[i] equals cipher[N] THEN
  38.             REPLACE text[i] with next cipher character
  39.          END IF
  40.          INCREMENT i
  41.       END WHILE
  42.       INCREMENT N
  43.       PRINT text at current step
  44.    END WHILE
  45. END IF
  46.  
  47.  
  48. */
  49.  
  50.  
  51.  
  52.  
  53. #include<stdio.h>
  54. #include<string.h>
  55.  
  56. int main(void)
  57. {
  58.    /*-----GATHER INPUT-----*/
  59.    
  60.    int cipherLen,textLen;
  61.    scanf("%d",&cipherLen);
  62.    char cipherText[cipherLen+1];
  63.    scanf("%16s",&cipherText);
  64.    
  65.    scanf("%d",&textLen);
  66.    char originalText[textLen+1];
  67.    scanf("%256c",&originalText);
  68.    
  69.    /*-----INPUT VALIDATION-----*/
  70.    
  71.    int validationPass=0;
  72.    char text[textLen+2];
  73.    char cipher[cipherLen+2];
  74.    text[textLen+2]="\0";
  75.    cipher[cipherLen+2]="\0";
  76.    
  77.    if (cipherLen>=2 && cipherLen<=16) /*Checks cipher length*/
  78.    {
  79.       if (textLen>=4 && textLen<=256) /*Checks text length*/
  80.       {
  81.          int i;
  82.          int allAlpha=1;
  83.          for (i=0;i<cipherLen;i++) /*Checks all chipher chars are alphabetical*/
  84.          {
  85.             if (isalpha(cipherText[i])==0) {allAlpha=0;}
  86.          }
  87.          if (allAlpha==1) /*Only continues to next check if previous ones have been passed*/
  88.          {
  89.             i=0;
  90.             int allUnique=1;
  91.             cipher[i]="Q";
  92.             i++;
  93.             cipher[i]=cipherText[i-1];
  94.             i++;
  95.             int j=0;
  96.             while (i<=cipherLen) /*Checks if all cipher characters are unique*/
  97.             {
  98.                for (j=0;j<=cipherLen;j++)
  99.                { if (cipherText[i-1]==cipherText[j] && i-1!=j) {allUnique=0;} }
  100.                cipher[i]=cipherText[i-1];
  101.                j=0;
  102.                i++;
  103.             }
  104.             if (allUnique==1)
  105.             {
  106.                allAlpha=1;
  107.                text[0]="Q";
  108.                for (i=1;i<=textLen;i++) /*Checks if all text characters are either alphabetical or a space*/
  109.                {
  110.                   if (isalpha(originalText[i])==0 && isspace(originalText[i])==0) {allAlpha=0;}
  111.                   text[i]=originalText[i];
  112.                }
  113.                if (allAlpha==1)
  114.                   {validationPass=1;} /*All validation has been passed*/
  115.                else
  116.                {printf("Original Text contains prohibited characters");} /*Error message for invalid text characters*/
  117.             }
  118.             else
  119.             {printf("Some cipher characters are not unique\n");} /*Error message for duplicate cipher characters*/
  120.          }
  121.          else
  122.          {printf("Some cipher key characters are not alphabetical");} /*Error message for invalid cipher charaters*/
  123.       }
  124.       else
  125.       {printf("Original text has an invalid length");} /*Error message for invalid orignal text length*/
  126.    }
  127.    else
  128.    {printf("Cipher text has an invalid length");} /*Error message for invalid cipher length*/
  129.    
  130.    /*-----ENCRYPTION-----*/
  131.    
  132.    
  133.    if (validationPass==1)
  134.    {
  135.       int N=1;
  136.       int i=1;
  137.       while (N<=cipherLen) /*Repeats for the length of the cipher*/
  138.       {
  139.          i=1;
  140.          if (N==cipherLen)
  141.          {printf("Step %d substitute %c with %c",N,cipher[N],cipher[1]);} /*If end of cipher has been reached, the first cipher char is used*/
  142.          else
  143.          {printf("Step %d substitute %c with %c",N,cipher[N],cipher[N+1]);} /*If end of cipher is not reached, the next cipher char is used*/
  144.          while (i<=textLen) /*Repeats for the length of the text*/
  145.          {
  146.             if (text[i]==cipher[N])
  147.             {
  148.                if (N==cipherLen)
  149.                {originalText[i]=cipher[1];} /*If end of cipher has been reached, the first cipher char is used*/
  150.                else
  151.                {originalText[i]=cipher[N+1];} /*If end of cipher is not reached, the next cipher char is used*/
  152.             }
  153.             i++;
  154.          }
  155.          N++;
  156.          printf("%s\n",originalText);
  157.       }
  158.    }
  159.    return 0;
  160. }