fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. int removerepeated(int size,int a[]);
  5. int insertelementat(int position,int a[],int size);
  6. void main()
  7. {
  8. int i,j,k,numstr[100],numcipher[100],numkey[100],lenkey,templen,tempkey[100],flag=-1,size,cipherkey[5][5],lennumstr,row1,row2,col1,col2;
  9. char str[100],key[100];
  10. printf("Enter a string\n");
  11. gets(str);
  12. //converting entered string to Capital letters
  13. for(i=0,j=0;i<strlen(str);i++)
  14. {
  15. if(str[i]!=' ')
  16. {
  17. str[j]=toupper(str[i]);
  18. j++;
  19. }
  20.  
  21. }
  22. str[j]='\0';
  23. printf("Entered String is %s\n",str);
  24. //Storing string in terms of ascii and to restore spaces I used -20
  25. size=strlen(str);
  26. for(i=0;i<size;i++)
  27. {
  28. if(str[i]!=' ')
  29. numstr[i]=str[i]-'A';
  30. }
  31. lennumstr=i;
  32. //Key processing
  33.  
  34. printf("Enter the key (Non repeated elements if possible)\n");
  35. gets(key);
  36. //converting entered key to Capital letters
  37. for(i=0,j=0;i<strlen(key);i++)
  38. {
  39. if(key[i]!=' ')
  40. {
  41. key[j]=toupper(key[i]);
  42. j++;
  43. }
  44. }
  45. key[j]='\0';
  46. printf("%s\n",key);
  47. //Storing key in terms of ascii
  48. k=0;
  49. for(i=0;i<strlen(key)+26;i++)
  50. {
  51. if(i<strlen(key))
  52. {
  53. if(key[i]=='J')
  54. {
  55. flag=8;
  56. printf("%d",flag);
  57. }
  58. numkey[i]=key[i]-'A';
  59. }
  60. else
  61. {
  62. if(k!=9 && k!=flag)//Considering I=J and taking I in place of J except when J is there in key ignoring I
  63. {
  64. numkey[i]=k;
  65. }
  66. k++;
  67.  
  68. }
  69. }
  70. templen=i;
  71. lenkey=removerepeated(templen,numkey);
  72. printf("Entered key converted according to Play Fair Cipher rule\n");
  73. for(i=0;i<lenkey;i++)
  74. {
  75. printf("%c",numkey[i]+'A');
  76. }
  77. printf("\n");
  78. //Arranging the key in 5x5 grid
  79. k=0;
  80. for(i=0;i<5;i++)
  81. {
  82. for(j=0;j<5;j++)
  83. {
  84. cipherkey[i][j]=numkey[k];
  85. k++;
  86. }
  87. }
  88.  
  89. printf("Arranged key\n");
  90. for(i=0;i<5;i++)
  91. {
  92. for(j=0;j<5;j++)
  93. {
  94.  
  95. printf("%c ",cipherkey[i][j]+'A');
  96.  
  97. }
  98. printf("\n");
  99. }
  100.  
  101. //Message Processing
  102.  
  103. for(i=0;i<lennumstr;i+=2)
  104. {
  105. if(numstr[i]==numstr[i+1])
  106. {
  107. insertelementat(i+1,numstr,lennumstr);
  108. lennumstr++;
  109. }
  110. }
  111. if(lennumstr%2!=0)
  112. {
  113. insertelementat(lennumstr,numstr,lennumstr);
  114. lennumstr++;
  115. }
  116. printf("Entered String/Message After Processing according to Play fair cipher rule\n");
  117. for(i=0;i<lennumstr;i++)
  118. {
  119. printf("%c",numstr[i]+'A');
  120. }
  121. for(k=0;k<lennumstr;k+=2)
  122. {
  123. for(i=0;i<5;i++)
  124. {
  125. for(j=0;j<5;j++)
  126. {
  127. if(numstr[k]==cipherkey[i][j])
  128. {
  129. row1=i;
  130. col1=j;
  131. }
  132. if(numstr[k+1]==cipherkey[i][j])
  133. {
  134. row2=i;
  135. col2=j;
  136. }
  137.  
  138. }
  139. }
  140. if(row1==row2)
  141. {
  142. col1=(col1+1)%5;
  143. col2=(col2+1)%5;
  144. numcipher[k]=cipherkey[row1][col1];
  145. numcipher[k+1]=cipherkey[row2][col2];
  146. }
  147. if(col1==col2)
  148. {
  149. row1=(row1+1)%5;
  150. row2=(row2+1)%5;
  151. numcipher[k]=cipherkey[row1][col1];
  152. numcipher[k+1]=cipherkey[row2][col2];
  153. }
  154. if(row1!=row2&&col1!=col2)
  155. {
  156. numcipher[k]=cipherkey[row1][col2];
  157. numcipher[k+1]=cipherkey[row2][col1];
  158. }
  159. }
  160. printf("\nCipher Text is\n");
  161.  
  162. for(i=0;i<lennumstr;i++)
  163. {
  164. printf("%c",numcipher[i]+'A');
  165. }
  166. printf("\n");
  167.  
  168.  
  169. }
  170.  
  171. int removerepeated(int size,int a[])
  172. {
  173. int i,j,k;
  174. for(i=0;i<size;i++)
  175. {
  176. for(j=i+1;j<size;)
  177. {
  178. if(a[i]==a[j])
  179. {
  180.  
  181. for(k=j;k<size;k++)
  182. {
  183. a[k]=a[k+1];
  184. }
  185. size--;
  186. }
  187. else
  188. {
  189. j++;
  190. }
  191. }
  192. }
  193. return(size);
  194. }
  195.  
  196. int insertelementat(int position,int a[],int size)
  197. {
  198. int i,insitem=23;
  199. int temp[size+1];
  200. for(i=0;i<=size;i++)
  201. {
  202. if(i<position)
  203. {
  204. temp[i]=a[i];
  205. }
  206. if(i>position)
  207. {
  208. temp[i]=a[i-1];
  209. }
  210. if(i==position)
  211. {
  212. temp[i]=insitem;
  213. }
  214.  
  215. }
  216.  
  217. for(i=0;i<=size;i++)
  218. {
  219. a[i]=temp[i];
  220. }
  221. return(size);
  222. }
Runtime error #stdin #stdout #stderr 0s 4552KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
./prog.sh: line 4: syntax error near unexpected token `('
./prog.sh: line 4: `int removerepeated(int size,int a[]);'