<mosaic.cnfolio.com>
PROGRAM
1. /*
2.
3. Final code with input errors handling
4.
5. START
6. Read length of cipher key
7.
8. IF key length is less than 2 or greater than 16
9.   THEN
10.     print key lenght error message
11.     ask for correct input length
12.     terminate program
13.   ELSE
14.     Read key and store it in an array
15. ENDIF
16.
17. IF given length and actual lenght of entered key is not equal
18.   THEN
19.     print message about mismatch in lenghts
21.     terminate program
22. ENDIF
23.
24. Read length of text to be encrypted and store it in text variable
25.
26. IF text lenght is less than 4 and greater than 256
27.   THEN
28.     print text lenght error message
29.     ask for correct input length
30.     terminate program
31.   ELSE
32.     read text and store it in an array
33. ENDIF
34.
35. IF given length and actual lenght of entered text is not equal
36.   THEN
37.     print message about mismatch in text lengths
39.     terminate program
40. ENDIF
41.
42. Declaration
43. integer verialbe i and j
44.
45. IF there is uppercase in key
46.   THEN
47.     print error message
48.     ask for correct input length
49.     terminate program
50. ENDIF
51.
52. IF there are uppercase in text
53.   THEN
54.     print error message
55.     ask for correct input length
56.     terminate program
57. ENDIF
58.
59. FOR i=0 to text_length-1 step 1
60. assign ith position in encrypted_text to ith position of text
61. ENDFOR
62.
63. FOR i=0 to i less than key_length-1 step 1
64.    IF i is less than keylength-1
65.    THEN
66.       FOR j=0 to j equals to text_length-1 step 1
67.          IF ith character of key equals to jth charcter of text
68.          THEN
69.          assign jth character of encrypted text to (i+1)th character of key
70.          ENDIF
71.       ENDFOR
72.       print step (i+1)th substitute character at ith key with character at (i+1)th key
73.
74.    ELSE
75.    FOR j=0 to j equals to text_length-1 step 1
76.          IF ith character of key equals to jth charcter of text
77.          THEN
78.          assign jth character of encrypted text to first character of key
79.          ENDIF
80.       ENDFOR
81.       print step (i+1)th substitute character at ith key with character at (i+1)th key
82.
83.    ENDIF
84. ENDFOR
85.
86. END
87.
88.
89. */
90.
91. #include <stdio.h>
92. #include <string.h>
93.
94. int main( void )
95. {
96.
97.   unsigned int key_length;
98.   unsigned int text_length;
99.
100.   char key[16] =   {'\0'};
101.   char text[256] = {'\0'};
102.   char encrypted_text[256] = {'\0'};
103.
104.   scanf("%d", &key_length);
105.   if(key_length<2 || key_length>16 )
106.     {
107.      printf( "Lenght of key is incorrect. It must be in the range of 2-16\n");
108.      printf( "Please make corrections in input and run program again\n");
109.      return 0;
110.     }
111.    else
112.      printf( "Lenght of cipher key is: %d\n",key_length);
113.
114.    scanf( "%s", key );
115.    printf( "Ciphter key is: %s\n",key);
116.
117.
118.    if(strlen(key)!=key_length)
119.    {
120.      printf( "There is mismatch in given lenght an actual length of given key\n");
121.      printf( "Please make corrections in input and run program again\n");
122.      return 0;
123.    }
124.
125.    scanf("%d", &text_length);
126.   if(text_length<4 || key_length>256 )
127.     {
128.      printf( "Lenght of tex is incorrect. It must be in the range of 4-256\n");
129.      printf( "Please make corrections in input and run program again\n");
130.      return 0;
131.     }
132.    else
133.      printf( "Lenght of text is: %d\n",text_length);
134.    scanf( " %[^\n]s" , &text); //Space before % is typo that just ignore any space before the string
135.
136.    printf( "text to be encrypted is: %s\n",text);
137.    if(strlen(text)!=text_length)
138.    {
139.      printf( "There is mismatch in given lenght and actual length of given text\n");
140.      printf( "Please make corrections in input and run program again\n");
141.      return 0;
142.    }
143.
144.   int i,j;
145.
146.    for(i=0; i<key_length;i++)
147.    {
148.      if (key[i] >= 'A' && key[i] <= 'Z')
149.          {
150.             printf( "There is some uppercase letters in the key text\n");
151.             printf( "Please make corrections in input and run program again\n");
152.             return 0;
153.          }
154.    }
155.
156.    for(i=0; i<text_length;i++)
157.    {
158.      if (text[i] >= 'A' && text[i] <= 'Z')
159.          {
160.             printf( "There is some uppercase letters in the input text\n");
161.             printf( "Please make corrections in input and run program again\n");
162.             return 0;
163.          }
164.    }
165.
166.     for(i=0; i<text_length;i++)
167.     encrypted_text[i]=text[i];
168.
169.    for(i=0; i<(key_length);i++)
170.    {
171.       if(i<(key_length-1))
172.       {
173.          for(j=0;j<text_length;j++)
174.             {
175.            if(key[i]==text[j])
176.                   encrypted_text[j]=key[i+1];
177.             }
178.          printf( "Step %d  Substitute %c with %c\n", i+1, key[i], key[i+1]);
179.          printf( "%s\n",encrypted_text);
180.       }
181.       else
182.       {
183.          for(j=0;j<text_length;j++)
184.             {
185.            if(key[i]==text[j])
186.                   encrypted_text[j]=key[0];
187.             }
188.          printf( "Step %d  Substitute %c with %c\n", i+1, key[i], key[0]);
189.          printf( "%s\n",encrypted_text);
190.       }
191.
192.
193.    }
194.
195. }