fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define SAFE printf("Safe\n");
  7. char * shift(char * str, int k)
  8. {
  9. char * decoded = (char *)malloc(251);
  10. int i;
  11. strcpy(decoded, str);
  12.  
  13. for(i=0; decoded[i]; i++)
  14. {
  15. int num;
  16. if(isalpha(decoded[i]))
  17. {
  18. num = decoded[i] - 'A' + 1;
  19. }
  20. else
  21. {
  22. num = 0;
  23. }
  24.  
  25. num+=k;
  26. if(num>26)
  27. num-=27;
  28.  
  29. if(!num)
  30. {
  31. decoded[i] = ' ';
  32. }
  33. else
  34. {
  35. decoded[i] = num + 'A' - 1;
  36. }
  37.  
  38.  
  39. }
  40. decoded[i] = 0;
  41.  
  42. return decoded;
  43. }
  44.  
  45. int countWords(char ** dictionary, char *str)
  46. {
  47. int count = 0;
  48. char word[21];
  49.  
  50. FILE * stream = fmemopen(str,strlen(str),"r");
  51.  
  52. while(fscanf(stream, "%s",word)>0)
  53. {
  54.  
  55. int i = 0;
  56. for(i; dictionary[i]; i++)
  57. {
  58. if(!strcmp(word,dictionary[i]))
  59. {
  60. //printf("%s matches %s in the dictionary!\n",word,dictionary[i]);
  61. count++;
  62. continue;
  63. }
  64. else
  65. {
  66. //printf("%s does not match %s in the dictionary\n",word,dictionary[i]);
  67. }
  68. }
  69. }
  70. fclose(stream);
  71. return count;
  72. }
  73.  
  74. int main()
  75. {
  76. char ** dictionary = (char **)malloc(100);
  77. memset(dictionary,0,100);
  78. int i = 0;
  79. while(1)
  80. {
  81. char * word = (char *)malloc(22);
  82. //scanf("%s",word);
  83. fgets(word, 22, stdin);
  84. word[strlen(word)-1] = 0;
  85. if(strcmp(word,"#"))
  86. {
  87. dictionary[i] = word;
  88. i++;
  89. }
  90. else
  91. {
  92. break;
  93. }
  94. }
  95.  
  96. /*
  97.  
  98.   dictionary input end
  99.  
  100.   */
  101.  
  102. char message[252];
  103. fgets(message, 252, stdin);
  104. message[strlen(message) - 1] = 0;
  105.  
  106. char ** decoded_strings = (char **)malloc(27);
  107. int k;
  108.  
  109. int match_count[27];
  110.  
  111. int max = 0;
  112. int max_words = 0;
  113. int index = 0;
  114. for(k=0; k<27; k++)
  115. {
  116. decoded_strings[k] = (char *)malloc(251);
  117. decoded_strings[k] = shift(message,k);
  118. match_count[k] = countWords(dictionary,decoded_strings[k]);
  119.  
  120. if(match_count[k]>max)
  121. {
  122. max = match_count[k];
  123. max_words = 1;
  124. index = k;
  125. }
  126. else if(match_count[k] == max)
  127. {
  128. max_words++;
  129. }
  130. }
  131.  
  132.  
  133.  
  134. if(max_words>1)
  135. {
  136. int i;
  137. int maxlen = 0;
  138. int ndex = 0;
  139. for(i=0; i<27; i++)
  140. {
  141.  
  142. if(match_count[i] == max)
  143. {
  144. int len = strlen(decoded_strings[i]);
  145. if(len>maxlen)
  146. {
  147. maxlen = len;
  148. ndex = i;
  149. }
  150. }
  151. }
  152.  
  153. printf("%s\n",decoded_strings[ndex]);
  154. }
  155. else if(max_words == 1)
  156. {
  157. printf("%s\n",decoded_strings[index]);
  158. }
  159. /*
  160.   printf("Dictionary:\n");
  161.   for(i=0; dictionary[i]; i++)
  162.   {
  163.   printf("%s\n",dictionary[i]);
  164.   }
  165.  
  166.   printf("Decoded strings:\n");
  167.   for(k=0; k<27; k++)
  168.   {
  169.   printf("%d %s %d\n",k,decoded_strings[k], countWords(dictionary,decoded_strings[k]));
  170.   }*/
  171.  
  172.  
  173.  
  174. //printf("Safe\n");
  175.  
  176. /*
  177.  
  178.   end
  179.  
  180.   */
  181.  
  182.  
  183. /*
  184.   free
  185.   */
  186.  
  187. // free dictionary
  188. for(i=0; dictionary[i]; i++)
  189. {
  190. free(dictionary[i]);
  191. }
  192. free(dictionary);
  193. //printf("Freed dictionary\n");
  194. //free decoded strings
  195.  
  196. for(i=0; decoded_strings[i]; i++)
  197. {
  198. free(decoded_strings[i]);
  199. }
  200. //printf("Freed decoded words\n");
  201. //free(decoded_strings);
  202. return 0;
  203. }
  204.  
Runtime error #stdin #stdout #stderr 0s 3436KB
stdin
THIS
DAWN
THAT
THE
ZORRO
OTHER
AT
THING
#
BUUBDLA PSSPABUAEBXO
stdout
ATTACK ZORRO AT DAWN
stderr
*** Error in `./prog': double free or corruption (out): 0x09f841b0 ***
======= Backtrace: =========
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x75e72)[0xb753be72]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(+0x76bb0)[0xb753cbb0]
./prog[0x8048823]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xf5)[0xb74df8f5]
./prog[0x8048865]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 1295706    /home/djIovn/prog
08049000-0804a000 rw-p 00000000 08:03 1295706    /home/djIovn/prog
09f84000-09fa5000 rw-p 00000000 00:00 0          [heap]
b74c4000-b74c6000 rw-p 00000000 00:00 0 
b74c6000-b766f000 r-xp 00000000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b766f000-b7670000 ---p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7670000-b7672000 r--p 001a9000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7672000-b7673000 rw-p 001ab000 08:03 1303839    /lib/i386-linux-gnu/i686/cmov/libc-2.17.so
b7673000-b7676000 rw-p 00000000 00:00 0 
b7676000-b7691000 r-xp 00000000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b7691000-b7692000 rw-p 0001a000 08:03 1303883    /lib/i386-linux-gnu/libgcc_s.so.1
b7692000-b7693000 rw-p 00000000 00:00 0 
b7693000-b76d4000 r-xp 00000000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76d4000-b76d5000 r--p 00040000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76d5000-b76d6000 rw-p 00041000 08:03 1303836    /lib/i386-linux-gnu/i686/cmov/libm-2.17.so
b76d6000-b77b2000 r-xp 00000000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77b2000-b77b3000 ---p 000dc000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77b3000-b77b7000 r--p 000dc000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77b7000-b77b8000 rw-p 000e0000 08:03 1345926    /usr/lib/i386-linux-gnu/libstdc++.so.6.0.18
b77b8000-b77bf000 rw-p 00000000 00:00 0 
b77c0000-b77c5000 rw-p 00000000 00:00 0 
b77c5000-b77c6000 r-xp 00000000 00:00 0          [vdso]
b77c6000-b77e5000 r-xp 00000000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b77e5000-b77e6000 r--p 0001f000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
b77e6000-b77e7000 rw-p 00020000 08:03 1303796    /lib/i386-linux-gnu/ld-2.17.so
bfb54000-bfb69000 rw-p 00000000 00:00 0          [stack]