-
-
Save anonymous/a83971720f9ac85e5186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(){ | |
//init vars | |
string fileLocation; | |
ifstream file; | |
string line; | |
char c; | |
char frequent; | |
int k; | |
int i; | |
char letters[26]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | |
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; | |
char lowerLetters[26]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | |
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; | |
//Ask the user for the file location | |
//and opening the contents of the file. | |
cout << "Enter the location of the file you'd like to decipher: "; | |
cin >>fileLocation; | |
file.open(fileLocation); | |
if(!file){ | |
cout <<"Error opening the file location" <<endl; | |
getchar(); | |
return 0; | |
} | |
frequent = frequencyCount(fileLocation); //This is where our function counts for the most frequent letter. | |
for(int i=0;i<26;i++){ | |
if(letters[i]==frequent){ | |
k = i-4; //Since frequent is what we are assuming to be 'E' when decyphered, | |
//when letters[i] also is equal to 'E', i-5 will give us the shift for the code. | |
} | |
} | |
while(file.get(c)){ //I put this here just to output the txt file for now | |
cout <<char(c); | |
} | |
getchar(); | |
getchar(); | |
return 0; | |
} | |
char frequencyCount(string fileLocation){ //This function will return the most frequently used letter, | |
char c; //which we are assuming to be an 'E' when decyphered. | |
int total[26]={0}; | |
int frequency = 0; | |
char frequent; | |
ifstream file; | |
file.open(fileLocation); | |
while(file.get(c)){ | |
if (isalpha(c)){ | |
c=toupper(c); | |
int index = c - 'A'; | |
total[index]++; | |
if(total[index] > frequency){ | |
frequency = total[index]; | |
frequent = char(index + 'A'); | |
} | |
} | |
} | |
return frequent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment