<mosaic.cnfolio.com>
PROGRAM
  1. /*
  2. Sudowoodo
  3. Start
  4.  
  5. ~~INPUT~~
  6. Get input of size of cipher key
  7. Get input of cipher key values "check"
  8.    Input validation:
  9.       Cipher must be between 2&16 values long
  10.       no duplicates
  11.       only lower case letters
  12.          If any validation rules broken
  13.          End Program
  14. Get input of phrase size
  15. Get input of phrase values "Text"
  16.    Input validation:
  17.       Phrase must be 4-256characters long
  18.       Can only contain lower case characters and spaces
  19. Copy cipher key values to another string "replace"
  20.  
  21. ~~Testing~~
  22. Set "check" & "Text" to initial positions
  23. Set "replace" to second position
  24. For "replace" is less than replacelength
  25.    For "Text" is less than Textlength
  26.       If "Text"="Check"
  27.          If TextPosition=UpdatedPostion
  28.             Don't update
  29.          Else   
  30.             Change "Text" value to "Replace" value
  31.             move "Text" one position
  32.             Add TextPosition to UdatedPositionTable
  33.       Else move "Text" one position
  34.    End For loop
  35.    Display current position of conversion
  36. End For loop
  37. Set "Replace" & "Text" to inital position
  38. Set "Check" to final positon -move 1 along-
  39. While "Text" is less than Textlength
  40.    If "Text"="Check"
  41.       Change "Text" value to "Replace" value
  42.       Move "Text" one position
  43.    Else move "Text" one position
  44. End While
  45. Display final conversion
  46. END sudowodo */
  47.  
  48. /* codowodo */
  49.  
  50. #include <stdio.h>
  51. #include <string.h>
  52.  
  53. int main(void)
  54. {
  55. /*Variables */
  56.  
  57. int Cipherlength; /*length of the cipher key */
  58. int Phraselength; /*length of the Text */
  59. char check[16];   /*String to hold the key which will be used to check the values in the text */
  60. char replace[16]; /*String to hold the key which will be used to replace the values in the text */
  61. char text[256];   /*String to hold the text to be coded, will be checked against the check string and replaced with the replace value */
  62. int CheckPosition=0; /*Variable used to hold and manipulate the position of the check string*/
  63. int ReplacePosition=0; /*Variable used to hold and manipulate the postion of the replace string*/
  64. int TextPosition=0; /*Variable used to hold and manipulate the position of the text string*/
  65. int RoundCounter=0; /*Variable used to count the number of cycles the program has looped for the output*/
  66. int ChangedPosition[256]; /*array to store the position of any value from the the text string to prevent that position being changed again*/
  67. int FreePosition=0;//* value used with the above array to store and track used positions in the text string */
  68. int Test;/*value used with the manipulation of the ChangedPosition array, without affecting the FreePosition variable*/
  69. int counter;/*A value used in detecting if a position was has been updated previously, if not equal to zero the value will not be updated*/
  70.  
  71. for(FreePosition=0; FreePosition<257; FreePosition++) /*Starts a loop to check each value in the array*/
  72.    { ChangedPosition[FreePosition]=257;/*Sets each value in the array to 257 which cannot naturally be achieved in the system*/
  73.    } /*this section of code operates here as if done after inputs it breaks the code somehow*/
  74.  
  75.  
  76. /* ---------- input ---------- */
  77. scanf ("%d", &Cipherlength); /*takes the input for the length of the cipherkey*/
  78.  
  79.    if(Cipherlength <= 'A' && Cipherlength >= 'z')/* error check to see if the entered value for length is a character*/
  80.      { /* the range goes from capital A to lower case z as ASCII codes for teh characters have capitals as a lower value
  81.         this code has been moved to before the check for length as to detect character values beforehand as it will treat it as a large
  82.           number rather than a character value triggering that error check and not this one*/
  83.      printf("ERROR: 10- The value entered for the length of the cipher key is not a number between 2 & 16, please correct this mistake and try again. \n");
  84.      return 0;
  85.      }
  86.    if(Cipherlength<2)/*checks if the code is too short*/
  87.       {
  88.       printf("ERROR: 1- You entered a length for the cipher that is less than 2, please enter a valid value and try again. \n");
  89.        return 0;
  90.       }
  91.    if(Cipherlength>16)/*checks if the code is too long*/
  92.       {
  93.       printf("ERROR: 2- You entered a length for the cipher that is greater than 16, please enter a valid value and try again. \n");
  94.        return 0;
  95.       }
  96. scanf ("%s", &check); /* takes the value for the cipher key*/
  97.  
  98.    if(strlen(check)!=Cipherlength) /*checks if inputed length matches the actual length of code */
  99.       {
  100.       printf("ERROR: 3- The length of the Cipher Key doesn't match the value given for the length of the cipher key, please correct this issue and try again. \n");
  101.        return 0;
  102.       }
  103.    for(Test=0; Test<Cipherlength; Test++) /*starts a loop with one postion in the cipher key*/
  104.       {
  105.       for(counter=Test+1; counter<Cipherlength+1; counter++)/*starts a second loop with a single position displacement from the previous loop*/
  106.          {
  107.          if(check[Test]==check[counter])/*if the values in the misaligned loops match there is a duplicate*/
  108.             {
  109.             printf("ERROR: 4- There is a duplicate value in the Cipher Key input, please correct this mistake and try again. \n");
  110.             return 0;}
  111.          }
  112.       }
  113.    for(CheckPosition=0; CheckPosition<Cipherlength; CheckPosition++)/*starts a loop to check each character in the cipher key input*/
  114.       {
  115.        if (check[CheckPosition] >= 'A' && check[CheckPosition] <= 'Z')/*Checks if any value of the cipher input is a capital*/
  116.            {
  117.             printf("ERROR: 5- There are capital values in the Cipher Key input, please correct this mistake and try again. \n");
  118.             return 0; }
  119.       }     
  120. scanf ("%d", &Phraselength); /*takes the value for the length of the message to be coded */
  121.  
  122.    if(Phraselength<4)/*checks if the inputted text to be coded is under the character limit*/
  123.       {
  124.       printf("ERROR: 6- The inputted message to be coded is less than 4 characters, please correct this mistake and try again. \n");
  125.       return 0;
  126.       }
  127.    if(Phraselength>256)/*checks if the inputted text to be coded is over the character limit*/
  128.       {
  129.       printf("ERROR: 7- The inputted message to be coded is over 256 characters, please correct this mistake and try again. \n");
  130.       return 0;
  131.       }
  132. scanf(" %[^\n]s", text); /* Takes the contents for the message to be coded the [^\n] is used to allow the scan to continue to after a space*/
  133.                          /* the space before the % is required or it will break everything*/
  134.    if(strlen(text)!=Phraselength)
  135.       {
  136.       printf("ERROR: 8- The inputted text doesn't match the length supplied, please correct this mistake and try again. \n");
  137.       return 0;
  138.       }
  139.    for(TextPosition=0; TextPosition<Phraselength; TextPosition++)/*starts a loop to check each character in the phrase input*/
  140.       {
  141.        if (text[TextPosition] >= 'A' && text[TextPosition] <= 'Z')/*Checks if any value of the phrase input is a capital*/
  142.            {
  143.             printf("ERROR: 9- There are capital values in the phrase input, please correct this mistake and try again. \n");
  144.             return 0; }
  145.       }
  146.      
  147. strncpy (replace,check, sizeof(replace)); /*copies the check string into the replace string for use in the comparison*/
  148.  
  149. /* ---------- Replacement (take 2) ---------- */
  150. ReplacePosition=0;
  151. CheckPosition=0;
  152. RoundCounter=0;/*Sets these variables to the initial position for the replacement testing*/
  153. FreePosition=0;
  154.  
  155. for(CheckPosition=0; CheckPosition<Cipherlength-1; CheckPosition++) /*starts a loop with the cipher key, to check each letter of the text*/
  156.    {
  157.       ReplacePosition++;/*each round of the loop increases these variables for the testing*/
  158.       RoundCounter++;
  159.      
  160.       for(TextPosition=0; TextPosition<Phraselength+1; TextPosition++) /*starts the loop with the phrase, the phrase will be looped(checked
  161.                                                                          against key) a number of times equal to the length of the key*/
  162.          {
  163.          if(text[TextPosition]==check[CheckPosition])/*checks if the text value is the same as the check variable, if true stats a new loop*/
  164.             {
  165.              Test=0;/*Resetting required variables to their required positions*/
  166.              counter=0;
  167.              for(Test=0; Test<FreePosition; Test++)/*Starts a loop by the number of values in the updated position array*/
  168.                { if (TextPosition==ChangedPosition[Test])/*Checks if the text position is inside the stored position array*/
  169.                    {
  170.                      counter++;}/*if there is a duplicate, add a value to counter*/
  171.                }     
  172.              if(counter==0)/*if the counter variable is equal zero/ no duplicates detected*/
  173.                   {
  174.                   text[TextPosition]=replace[ReplacePosition];/*Updates the text in the current text position*/
  175.                   ChangedPosition[FreePosition]=TextPosition;/*adds the current text postion into the used position array*/
  176.                   FreePosition++; }/*moves the free position variable to the next "empty" spot*/
  177.                }
  178.          }
  179.          printf("Step %d replace %c with %c \n", RoundCounter, check[CheckPosition], replace[ReplacePosition]);/*First line of the output*/
  180.          printf("%s \n", text); /*second line in the required output*/
  181.    }
  182. /*setting the values to their final positions for the last round of testing*/   
  183. ReplacePosition=0;
  184. CheckPosition=Cipherlength-1; /* the minus one is to get set the value in teh right position for this round of testing for reasons*/
  185. TextPosition=0;
  186. RoundCounter=Cipherlength;
  187.  
  188. /*This section of code is a repeate of the previous loop to check the final positions for the code*/
  189.  
  190. for(TextPosition=0; TextPosition<Phraselength+1; TextPosition++)
  191.    { if(text[TextPosition]==check[CheckPosition])
  192.       { Test=0;
  193.              counter=0;
  194.              for(Test=0; Test<FreePosition; Test++)
  195.                { if (TextPosition==ChangedPosition[Test])
  196.                    {
  197.                      counter++;}
  198.                }     
  199.              if(counter==0)
  200.                   {
  201.                   text[TextPosition]=replace[ReplacePosition];
  202.                   ChangedPosition[FreePosition]=TextPosition;
  203.                   FreePosition++; }
  204.       }
  205.       }
  206. /*final Output*/
  207. printf("Step %d replace %c with %c \n", RoundCounter, check[CheckPosition], replace[ReplacePosition]);
  208. printf("%s \n", text);   
  209.      
  210. return 0;
  211. }