<mosaic.cnfolio.com>
PROGRAM
  1. /*        PSEUDO CODE
  2. START
  3.                 / input recognation and error detection /
  4.       get the cipher key text lentgh
  5.     IF
  6.        2 <= the cipher key text lentgh <= 16
  7.          THEN
  8.            get the cipher key text
  9.        END IF
  10.     IF
  11.       the cipher key text == the cipher key text lentgh AND the characters in the cipher key text is from a-z lower case without duplicates
  12.          THEN
  13.             get the plain text lentgh
  14.        END IF
  15.        IF
  16.        4 <= the plain text lentgh <= 256
  17.          THEN
  18.            get the plain text
  19.        END IF
  20.     IF
  21.       the plain text == the plain text lentgh AND the characters in the plain text is including the spaces and alphabet from a-z lower case
  22.                   / loop
  23.          THEN
  24.             get the first character of the cipher key text and compare it with the plain text characters
  25.                IF
  26.                   the first character of the cipher key text equal to one of the plain text characters
  27.                    THEN
  28.                        replace the characters that are matching the first character of the cipher key text with the following character in the cipher key text
  29.                 /repeat for the next character in the cipher key text and keep giving output for each replacement
  30.                 and when you reach the last character of the cipher key text and it matches then replace in the plain text with the first character of the cipher key text
  31.         END IF
  32. END
  33. */
  34.              /*source code */
  35.  
  36. #include <stdio.h>
  37. /*standard library*/
  38. #include <string.h>
  39. /*I used the string library to enable the usage of the string functions such us : strlen, strcpy,and strchr*/
  40. int main(void)    /* To enable the start of the main function */
  41. {
  42.      int    LengthOfTheCipherKey; /* To introduce the first input that is the length of the cipher key string */
  43.      char   StringCipherKey[16]; /* To be able to look for specific positions in the cipher key */
  44.      int    LengthOfThePlainText; /* To introduce the third input that is the length of the plain text string */
  45.      char   StringPlainText[256]; /* To be able to look for specific positions in the plain text */
  46.      char   StringPlainTextTemporary[256]; /*This is introduced to be used as the plain text after the changes so we keep printing it after each step */
  47.      int    N; /* To enable the localisation of the current value in the cipher key */
  48.      int    TargetValue; /* To use it to scan for it in the plain text   */
  49.      int    SubstitutionValue; /*To use it during the substitution of the target value found in the plain text*/
  50.      int    ReplacedCharacter; /* To check if the target value exists in the plain text */
  51.      int    i; /*To enable the localisation of the current value in the temporary plain text and make changes in it */
  52.      
  53.          scanf("%d", &LengthOfTheCipherKey); /* To know the length of the cipher key in ordre to use it in the if statement */
  54.         // printf ("The Length Of The Cipher Key is : %d \n\n ",LengthOfTheCipherKey  );
  55.          scanf (" %[a-z]s", &StringCipherKey)/* To get only the lower case cipher key's characters */
  56.         // printf ("The Cipher Key is : %s \n\n",StringCipherKey);
  57.      if (LengthOfTheCipherKey <=16 && LengthOfTheCipherKey>=2 && LengthOfTheCipherKey==strlen(StringCipherKey)) /*To make some limitations in terms of the lenght of the cipher key*/
  58.      {
  59.          scanf("%d", &LengthOfThePlainText); /* To know the length of the plain text in ordre to use it in the if statement*/
  60.          // printf ("The Length Of The plain text is : %d \n\n",LengthOfThePlainText  );
  61.          scanf(" %[^\t\n]s[a-z]s",&StringPlainText); /* To get the lower case characters and the spaces included in the plain text */
  62.          // printf ("The plain text is : %s \n\n",StringPlainText  );           
  63.         strcpy(StringPlainTextTemporary, StringPlainText); /*to copy the contents of the original plain text into the temporary plain text*/
  64.         //printf ("The plain text temporary is : %s \n\n",StringPlainTextTemporary  );
  65.        
  66.        
  67.      if (LengthOfThePlainText <=256 && LengthOfThePlainText>=4 && LengthOfThePlainText==strlen(StringPlainText)) /*To make some limitations in terms of the lenght of the plain text*/
  68.      {
  69.      for(N=1; (N <= strlen(StringCipherKey)); N++) /*To keep adding 1 to the variable N as long as it is less or equal to the lentgh of the cipher key*/
  70.            {
  71.         for(N=1; (N < strlen(StringCipherKey)); N++) /*To keep adding 1 to the variable N as long as it is less than the lentgh of the cipher key*/
  72.            {
  73.            TargetValue= StringCipherKey[N-1]; /*To identifie the value of the target value*/
  74.            scanf("%c", &TargetValue);/*To look for and store the value of the target value*/
  75.            //printf ("target value is %c \n", TargetValue);
  76.            SubstitutionValue= StringCipherKey[N];/*To identifie the value of the substitution value*/
  77.            scanf("%c", &SubstitutionValue);/*To look for and store the value of the substitution value*/
  78.            //printf ("SubstitutionValue is %c \n \n",  SubstitutionValue);
  79.            scanf("%d", &N); /*to look for the value of N and use it as the number of the steps in the next ligne*/
  80.            printf("step %d substitute %c with %c \n \n",N,TargetValue,SubstitutionValue);/*to print the steps showing in the output*/
  81.            for ( i=1 ; i<=LengthOfThePlainText ;i++ )/*To keep adding 1 to the variable i as long as it is less than or equal to the lentgh of the plain text*/
  82.                   {
  83.                   ReplacedCharacter=(strchr(StringPlainText, TargetValue));/*to look for the target value in the original plain text */
  84.                      if ( StringPlainText[i-1] == TargetValue )/*to check if the target value is equal to a position in the original plain text*/ 
  85.                      {
  86.                         StringPlainTextTemporary[i-1]=SubstitutionValue;/*to subsitute the position that equals the target value by the substitution value in the temporary plain text*/
  87.                      }
  88.                   }
  89.                    printf("%s \n\n", StringPlainTextTemporary);/*to print the temporary plain text*/
  90.            }
  91.            scanf("%d", &N); /*to look for the value of N and use it as the number of the steps */
  92.         if (N==strlen(StringCipherKey))
  93.         {
  94.            TargetValue= StringCipherKey[N-1];/*To identifie the value of the target value*/
  95.            scanf("%c", &TargetValue);/*To look for and store the value of the target value*/
  96.            SubstitutionValue= StringCipherKey[0];/*To identifie the value of the substitution value*/ 
  97.            scanf("%c", &SubstitutionValue);/*To look for and store the value of the substitution value*/
  98.            printf("step %d substitute %c with %c \n \n",N,TargetValue,SubstitutionValue); /*to print the steps showing in the output*/
  99.            for ( i=1 ; i<=LengthOfThePlainText ;i++ )/*To keep adding 1 to the variable i as long as it is less than or equal to the lentgh of the plain text*/
  100.                   {
  101.                   ReplacedCharacter=(strchr(StringPlainText, TargetValue));/*to look for the target value in the original plain text */
  102.                      if ( StringPlainText[i-1] == TargetValue )/*to check if the target value is equal to a position in the original plain text*/ 
  103.                      {
  104.                         StringPlainTextTemporary[i-1]=SubstitutionValue;/*to subsitute the position that equals the target value by the substitution value in the temporary plain text*/
  105.                      }
  106.                   }
  107.                    printf("%s \n\n", StringPlainTextTemporary);/*to print the temporary plain text*/
  108.         }         
  109.            }         
  110.      }
  111.      else
  112.         printf("check the length of the plain text and compare it with the string of the plain text");/*To make some limitations in terms of the lenght of the plain text*/
  113.      }
  114.    else
  115.         printf("check the length of the cipher key and compare it with the string of the cipher key");/*To make some limitations in terms of the lenght of the cipher key*/
  116.        }