Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 300
- #define FLUSH stdin=freopen(NULL, "r", stdin)
- #define RMN(str) str[strcspn(str,"\n")] = 0
- void frequencyAnalysis(char[], char);
- void search(char*, char*);
- void decrypt(char, char[]);
- int main(int argc, char** argv) {
- char str[SIZE];
- char res[SIZE];
- char flag;
- do{
- flag = 0;
- printf("Input number string with length of %d ", SIZE);
- fgets(str, sizeof(str), stdin);
- FLUSH;
- RMN(str);
- if(strlen(str) < 1 || strlen(str) > SIZE){
- printf("please try again");
- flag = 1;
- }
- }while (flag);
- search(str, res);
- FLUSH;
- printf("most frequent char is %s", res);
- return 0;
- }
- void search(char *string, char* threeMostCommonChars){
- int count[SIZE] = {0}, i;
- for(i = 0; string[i]; i++){
- if(string[i] != ' '){
- (count[string[i]])++;
- }
- }
- int first = 0, second = 0, third =0;
- for(i = 0; i < SIZE; i++){
- if(count[i] > count[first]){
- third = second;
- second = first;
- first = i;
- }
- else if(count[i] > count[second] &&
- count[i] != count[first]){
- third = second;
- second = i;
- }
- else if(count[i] > count[third] &&
- count[i] != count[second]){
- third = i;
- FLUSH;
- }
- }
- char mostCommonChars[3] = {first, second, third};
- FLUSH;
- strcpy(threeMostCommonChars, mostCommonChars);
- }
- void frequencyAnalysis(char string[], char mostFrequentChar){
- char shift = 'e' - mostFrequentChar;
- decrypt(shift, string);
- printf("\nchanging %c->%c(shift %d) %s", mostFrequentChar, 'e', shift, string);
- }
- void decrypt(char shift, char string[]){
- //reference problem2_initial.txt
- size_t i = 0;
- while(string[i]){
- if(string[i] >= 'a' && string[i]<= 'z'){
- string[i] = (string[i] + shift - 'a')%26 + 'a';
- }
- if(string[i] >= 'A' && string[i]<='Z'){
- string[i] = (string[i] + shift - 'A')%26 + 'A';
- }
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement