<mosaic.cnfolio.com>
PROGRAM
  1. /*pseudo code
  2.  
  3. start
  4.  
  5.  
  6. input cipher length
  7. input cipher text
  8.  
  9. check cipher length inputted is between 2 and 16
  10. check cipher text is the same legth as the one stated
  11. check cipher length doesnt have repeating leters
  12. check cipher is all lower case letters
  13.  
  14. input text length
  15. input text
  16.  
  17. check text length inputted is between 4 and 256
  18. check text is sam esize as inputted length
  19. check text is all lower case letters
  20.  
  21.  
  22. check text for letters matching the first cipher letter
  23. swap found letters for second cipher letter
  24. print step number with changed text
  25. repeat these steps untill cipher reaches last letter
  26. then swap all found letters with first letter in cipher
  27. print final step number and completly changed text
  28.  
  29.  
  30. */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35.  
  36. char CIPHER[17]; //cipher text array//
  37. char TEXT[256]// text array//
  38. char NEW[256];   //outputted text array//
  39. int cipherlength, textlength; // lengths for text and cipher//
  40. char hold;       // this holds the letter that is being changed//
  41. int i,N,j,x,y,k; // this are all used to hold numbers for for loops//
  42.  
  43. int main (void)
  44. {
  45.    scanf("%d",&cipherlength); // scans for the cipher length//
  46.  
  47.  
  48.     if (cipherlength >=2 && cipherlength <=16)  // this checks the number entered is between 2 and 16//
  49.    {
  50.  
  51.    scanf ("%s",CIPHER);// this scans the text for the cipher//
  52.  
  53.  
  54.    for(x = 0; CIPHER[x] != '\0'; x++);// this finds the length of the cipher text entered//
  55.    if (x == cipherlength) // this checks the if the cipher text length matches the one stated =//
  56.    {
  57.     k=0; // this sets the error flag to 0//
  58.    for (y=0;y<=cipherlength-1;y++)  // this for loop is used to check that all the letters are lower case letters//
  59.        {
  60.  
  61.     if (CIPHER[y]<'a' || CIPHER[y]>'z')
  62.  
  63.     {
  64.          k=1; // if the letter is not lower case then the error flag is set to 1//
  65.     }
  66.        }
  67.      if (k==0)// this checks the state of the error flag.
  68.      {
  69.  
  70.     N=0;
  71.     k=0; //resets error flag//
  72.     while (N<cipherlength)// this while and for loop is used to check that there are no repeated letters in the cipher text//
  73.     {
  74.      for (y=0;y<=cipherlength;y++)
  75.      if ((CIPHER [N] == CIPHER [y])&& (N != y))
  76.      {
  77.        k=1; //if there are repeats the error flag is set to 1//
  78.      }
  79.       N++; //this is used to cycle through the letters being checked//
  80.     }
  81.  
  82.     if (k==0)//checks error flag//
  83.     {
  84.  
  85.    scanf ("%d",&textlength);//this scans for the text length//
  86.  
  87.  
  88.  
  89.  
  90.   if (textlength >=4 && textlength <=256) // check if text is between 4 and 256//
  91.    {
  92.  
  93.    scanf ("\n%[^\n]",TEXT);// this scans for the text until a new line has been found//
  94.  
  95.    k=0;
  96.  
  97.    for(y = 0; TEXT[y] != '\0'; y++);// this counts the length of the text given//
  98.  
  99.    if (y == textlength) // this checks that the text length matches the stated length//
  100.    {
  101.  
  102.        for (x=0;x<=textlength-1;x++) //this for loop checks all letters entered are lower case//
  103.     {
  104.  
  105.     if ((TEXT[x]<'a' || TEXT[x]>'z')&& TEXT[x]!= 32)
  106.     {
  107.  
  108.      k=1;//sets the error flag to 1//
  109.  
  110.     }
  111.  
  112.     }
  113.  
  114.     if (k==0)//checks the error flag//
  115.     {
  116.    for (i=0;i<textlength;i++) // this loads the entire text into the output array ready to be swapped//
  117.     {
  118.    NEW[i]=TEXT[i];
  119.    }
  120.  
  121.  
  122.    CIPHER [cipherlength] = CIPHER [0];// this sets the first letter of the cipher to the knew
  123.    N=0;
  124.    while(N<cipherlength) // this while loop is what does the exchanging of letters//
  125.    {
  126.      for(i=0;i<textlength;i++)// this for loop checks every letter against the cipher key//
  127.      {
  128.       if (CIPHER[N] == TEXT[i]) // this checks the text letter to the cipher letter//
  129.       {
  130.  
  131.          hold = CIPHER[N+1]; //this makes hold become the letter that will be put in the text//
  132.          NEW[i]= hold;  //this places the held letter into the output array//
  133.       }
  134.  
  135.     }
  136.  
  137.      printf ("\nstep %d substitute %c with %c \n", N+1, CIPHER [N], CIPHER[N+1]);// this prints the step by step of what letters are being swapped//
  138.      for (j=0;j<textlength;j++)//this for loop is used to print of the new text after the substitution has taken place//
  139.          {
  140.         printf ("%c",NEW[j]);
  141.          }
  142.     N++;
  143.    }
  144.     }
  145.     else
  146.     {
  147.      printf ("\nerror only lower case letters can be entered for the text, please try again ");//error message//
  148.     }
  149.    }
  150.    else
  151.    {
  152.     printf ("\nerror text entered did not match length stated, please try again\n");//error message//
  153.    }
  154.    }
  155.    else
  156.    {
  157.        printf("\nerror one numbers between 4-256 can be entered for text length,please try again\n");//error message//
  158.    }
  159.     }
  160.     else
  161.     {
  162.    printf("\nerror letter cannot be repeated in the cipher key, please try again\n");//error message//
  163.     }
  164.    }
  165.    else
  166.    {
  167.    printf ("\nerror only lower case letters allowed in cipher key, please try again\n");//error message//
  168.    }
  169.    }
  170.  
  171.    else
  172.    {
  173.     printf ("\nerror cipher length entered did not match length stated, please try again\n");//error message//
  174.    }
  175.    }
  176.    else
  177.    {
  178.     printf("\nerror cipher must be between 2-16, please try again\n");//error message//
  179.    }
  180.  
  181. }