Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 300
  5. #define FLUSH stdin=freopen(NULL, "r", stdin)
  6. #define RMN(str) str[strcspn(str,"\n")] = 0
  7.  
  8. void frequencyAnalysis(char[], char);
  9. void search(char*, char*);
  10. void decrypt(char, char[]);
  11.  
  12. int main(int argc, char** argv) {
  13. char str[SIZE];
  14. char res[SIZE];
  15. char flag;
  16. do{
  17. flag = 0;
  18.  
  19. printf("Input number string with length of %d ", SIZE);
  20. fgets(str, sizeof(str), stdin);
  21. FLUSH;
  22. RMN(str);
  23.  
  24. if(strlen(str) < 1 || strlen(str) > SIZE){
  25. printf("please try again");
  26. flag = 1;
  27. }
  28. }while (flag);
  29.  
  30. search(str, res);
  31. FLUSH;
  32. printf("most frequent char is %s", res);
  33.  
  34. return 0;
  35. }
  36.  
  37. void search(char *string, char* threeMostCommonChars){
  38. int count[SIZE] = {0}, i;
  39. for(i = 0; string[i]; i++){
  40. if(string[i] != ' '){
  41. (count[string[i]])++;
  42. }
  43. }
  44.  
  45. int first = 0, second = 0, third =0;
  46.  
  47. for(i = 0; i < SIZE; i++){
  48. if(count[i] > count[first]){
  49. third = second;
  50. second = first;
  51. first = i;
  52. }
  53. else if(count[i] > count[second] &&
  54. count[i] != count[first]){
  55. third = second;
  56. second = i;
  57. }
  58. else if(count[i] > count[third] &&
  59. count[i] != count[second]){
  60. third = i;
  61. FLUSH;
  62. }
  63. }
  64. char mostCommonChars[3] = {first, second, third};
  65. FLUSH;
  66. strcpy(threeMostCommonChars, mostCommonChars);
  67. }
  68.  
  69.  
  70. void frequencyAnalysis(char string[], char mostFrequentChar){
  71. char shift = 'e' - mostFrequentChar;
  72. decrypt(shift, string);
  73. printf("\nchanging %c->%c(shift %d) %s", mostFrequentChar, 'e', shift, string);
  74. }
  75.  
  76.  
  77. void decrypt(char shift, char string[]){
  78. //reference problem2_initial.txt
  79. size_t i = 0;
  80. while(string[i]){
  81. if(string[i] >= 'a' && string[i]<= 'z'){
  82. string[i] = (string[i] + shift - 'a')%26 + 'a';
  83. }
  84. if(string[i] >= 'A' && string[i]<='Z'){
  85. string[i] = (string[i] + shift - 'A')%26 + 'A';
  86. }
  87. i++;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement