<mosaic.cnfolio.com>
PROGRAM
1. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Pseudo Code~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.
3. START
4.    Read the length of the cipher key
5.    Read the cipher key
6.    Read the length of the text
7.    Read the text
8.
9.    IF the length of the text equals the inputted text
10.       THEN
11.          Read the plain text
12.    ENDIF
13.
14.    IF the length of the cipher key is less than 2
15.       Output errror message "Increase the length of your cipherkey"
16.    ENDIF
17.
18.    IF the length of the cipher key is more than 16
19.       Output errror message "Decrease the length of your cipherkey"
20.    ENDIF
21.
22.    IF the length of the cipher key doesn't equal the cipher key
23.       Output error message "Re-enter the length of your cipher key"
24.    ENDIF
25.
26.    IF the cipher key isn't between a-z
27.       THEN
28.          Output error message "The cipher key contains invalid characters"
29.    ENDIF
30.
31.    IF the length of the plain text is less than 4
32.       THEN
33.          Output error message "The length of the plain text is too small;increase the plain text"
34.    ENDIF
35.
36.    IF the length of the plain text is greater than 256
37.       THEN
38.          Output error message "The length of the plain text is too big, decrease the plain text'
39.    ENDIF
40.
41.    IF the length of the plain text doesn't equal the inputted plain text
42.       Output error message "Re-enter the length of your plain text"
43.    ENDIF
44.
45.    IF plain text contains characters that aren't between a-z
46.       THEN
47.          Output error message "The plain text contains invalid characters"
48.    ENDIF
49.
50.       WHILE we have characters in the cipher key, use N to indicate the current letter of the cipher key
51.       IF any of the letters in the text string equal the current letter of the cipher key
52.          THEN
53.             Replace these letters with the current cipher key plus one
54.       ENDIF
55.       Move to the next letter in the cipher
56.       IF its the last character in the cipher key that we are using
57.          THEN
58.             Instead of using the next cipher key (N+1), use the first letter of the cipher key
59.       ENDIF
60.    ENDWHILE
61. END */
62.
63.
64. #include <stdio.h>
65. #include <string.h>
66.
67. int main(void)
68. {
69.   int lengthofcipher;
70.   char cipherkey[15] = {'\0'};
71.   int lengthoftext;
72.   char text[255] = {'\0'};
73.   int nextcipherletter;
74.
75. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Input~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76.
77.   scanf("%d",&lengthofcipher); // This reads the 1st line, which is the length of the cipher key
78.
79.   scanf("%s", &cipherkey); // This reads the 2nd line, which is the cipher key
80.
81.   scanf("%d", &lengthoftext); //This reads the 3rd line, which is length of the plain text
82.
83.   scanf("%c"); //gets rid of the linebreak
84.   scanf("%[^\n]%s", &text); //This reads the 4th line which is the plain text, up until the end of the line. [/n] is the character we will stop reading at
85.
86.   if(text[0] == '\n') // This step is checking if the first character is a line break
87.   {
88.    for(int currentletter=0; currentletter<strlen(text); currentletter++) // This step moves all the characters along one to fill the previous space taken up by the line break
89.    {
90.     text[currentletter] = text[currentletter+1];
91.    }
92.   }
93.
94.
95. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Error Messages~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96.
97.
98. //error messages for the length of the cipher key not being between required length
99.
100.
101.    if( lengthofcipher<2)
102.    {
103.       printf("The current length of the cipher is %d. Increase the length of your cipher making it above 2 and less than 16", lengthofcipher);
104.       return 0;
105.    }
106.
107.    if( lengthofcipher>16)
108.    {
109.       printf("The current length of the cipher is %d. Decrease the length of your cipher making it above 2 and less than 16", lengthofcipher);
110.     return 0
111.    }
112.
113.
114.
115. //error message for the cipher key not being the length as previously stated
116.
117.
118.
119.    if( lengthofcipher != strlen(cipherkey)) //This compares the stated length of the cipher to the actual length
120.    {
121.       printf("The length of the cipherkey is different to the length stated, re enter the length of the cipherkey");
122.       return 0;
123.    }
124.
125.
126. //error message to prevent the cipher key containing any characters that arent between a-z
127.
128.
129.   for( int currentcipherletter =0; currentcipherletter <lengthofcipher; currentcipherletter ++)//this step is looping through the cipher to ensure all the characters are between a-z
130.    {
131.
132.      if ((cipherkey[currentcipherletter]< 'a') || (cipherkey[currentcipherletter]> 'z'))//if the cipher key contains characters that arent between a-z then produce an error message
133.      {
134.         printf("The cipher key text contains invalid characters, only lower characters possible between a-z");
135.         printf("\n\ninvalid chars are:%c\n", cipherkey[currentcipherletter]);
136.         return 0;
137.
138.      }
139.    }
140.
141.
142. //error message for duplicate letters in cipherkey
143.
144.
145.
146.   for(int currentcipherletter=0; currentcipherletter<lengthofcipher; currentcipherletter++) //this loop is going through the cipherkey 1 by 1
147.    {
148.       for(int remainingcipherletters=currentcipherletter+1; remainingcipherletters<lengthofcipher; remainingcipherletters++)//inside this loop the current letter of the cipher key is compared against the rest of the letters for dupilcates
149.       {
150.          if(cipherkey[currentcipherletter] == cipherkey[remainingcipherletters])
151.          {
152.             printf("The cipherkey contains duplicate letters, re enter the cipherkey\n");
153.             return 0;
154.          }
155.       }
156.    }
157.
158.
159.
160. // error messages for the length of the plain text not being between the required values
161.
162.
163.
164.    if( lengthoftext<4)
165.    {
166.       printf("The current length of the text is %d. Increase the length of your text making it above 4 and less than 256", lengthoftext);
167.       return 0;
168.    }
169.
170.    if( lengthoftext>256)
171.    {
172.       printf("The current length of the text is %d. Decrease the length of your text making it above 4 and less than 256", lengthoftext);
173.       return 0;
174.    }
175.
176.
177.
178. //error messages for the length of the plain text not being the same as stated previously
179.
180.
181.
182.    if( lengthoftext != strlen(text))
183.    {
184.       printf("The length of the plain text is different to the length stated, decrease the length of the plain text");
185.       return 0;
186.    }
187.
188.
189. //error message to prevent the text containing any characters that arent between a-z
190.
191.
192.
193.    for( int currenttextletter =0; currenttextletter <=lengthoftext-1; currenttextletter ++)
194.    {
195.
196.      if (((text[currenttextletter]<'a') || (text[currenttextletter]>'z')) && (text[currenttextletter]!=' ')) //check letters are between a and z and ignore spaces
197.      {
198.         printf("The plain text contains invalid characters, only lower characters are possible between a-z");
199.         return 0;
200.
201.      }
202.    }
203.
204.
205.
206. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Making a copy of the text~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207.
208.
209. char copytext[255] = {'\0'}; //making a copy of the text so that characters are changed from the original text rather than
210.
211. strcpy(copytext, text);
212.
213.
214.
215. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Output~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216.
217.
218.   for(int currentletterofcipherkey=0; currentletterofcipherkey <lengthofcipher; currentletterofcipherkey ++) //this step is passing through the cipher one letter at a time, length of cipher plus one to account for array starting at zero
219.   {
220.
221.
222.     if(cipherkey[currentletterofcipherkey]!=cipherkey[strlen(cipherkey)-1])//if its not the last letter in the cipher being used
223.       {
224.
225.          for(int currenttextletter=0; currenttextletter <lengthoftext; currenttextletter ++)
226.          {
227.
228.             if(text[currenttextletter]==cipherkey[currentletterofcipherkey]) //the programme is comparing the text letters with the cipherkey letters
229.               {
230.                 copytext[currenttextletter]=cipherkey[currentletterofcipherkey+1];
231.
232.
233.               }
234.          }
235.
236.          printf("Step %d: Substitute %c with %c\n", (currentletterofcipherkey+1), cipherkey[currentletterofcipherkey], cipherkey[currentletterofcipherkey+1]);
237.           printf("%s\n",copytext);
238.       }
239.       else
240.       {
241.
242.          //nextcipherletter = cipherkey[n+1]; //if the letters are the same the programme is swapping the text letters to the next cipherkey that was compared
243.          for(int currenttextletter=0; currenttextletter<lengthoftext; currenttextletter++)
244.          {
245.
246.             if(text[currenttextletter]==cipherkey[currentletterofcipherkey]) //the programme is comparing the text letters with the cipherkey letters
247.               {
248.                 copytext[currenttextletter]=cipherkey[0];
249.
250.
251.               }
252.          }
253.
254.          printf("Step %d: Substitute %c with %c\n", (currentletterofcipherkey+1), cipherkey[currentletterofcipherkey], cipherkey[0]);
255.           printf("%s\n",copytext);
256.       }
257.
258.
259.
260.
261.
262.   }
263.
264.
265. }