rendered paste body/* ========================================================================== */
/* */
/* myVig.cpp */
/* Brian Marsh */
/* */
/* Simple vigenere's cipher program */
/* */
/* ========================================================================== */
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <exception>
using namespace std;
void getFreq( int keyLen, int offsetBy, char cipher[], vector<double>& freq);
double index(string cipherText, int colSize, int offset);
void keyLength(string cipher);
int main() {
string cipher;
int len;
double ic;
cin >> cipher;
keyLength(cipher);
return 0;
}
//*/ //BLOCK COMMENT
void keyLength(string cipher){
char cipherArray[cipher.size()];
strncpy(cipherArray, cipher.c_str(),cipher.size());
int guess = 2;
int largestKey = (int)sqrt(cipher.size());
//cout <<"outer for begins\n";
for (int keyLen = 2; keyLen <= largestKey; keyLen++) {
//cout <<"inner for begins\n";
for (int off = 0; off < keyLen; off++) {
//cout <<"before index function\n";
double indexVal = index(cipher, keyLen, off);
//cout << "Key Len = "<<keyLen<<", Offset = "<<off<<", IC = "<<indexVal<<endl;
}
}
}
//*/ //END BLOCK COMMENT
//*/ //BLOCK COMMENT
double index(string cipherText, int colSize, int offset){
//cout <<"in Index function\n";
//Convert string to array of chars
char cipherArray[cipherText.size()];
strncpy(cipherArray, cipherText.c_str(),cipherText.size());
// initialize variables
double len = 0;
double ic = 0;
vector<double> holding;
vector<double> freq;
//cout <<"before getFreq\n";
getFreq(colSize, offset, cipherArray, freq);
//cout <<"afterGetFreq\n";
holding = freq;
for (int i = 0; i < 26; i++){
ic = ic + holding[i]*(holding[i]-1);
len = len + holding[i];
}
return ic/(len*(len-1));
}
//*/ //END BLOCK COMMENT
//*/ //BLOCK COMMENT
void getFreq( int keyLen, int offsetBy, char cipher[], vector<double>& freq){
//cout <<"in getFreq Function\n";
vector<double> fillMe(26);
//cout <<"before For \n";
for (int i = offsetBy; i < sizeof(cipher); i+=keyLen){
//cout <<"getFreq for loop. i = "<<i<<endl;
try {
cout<<"-------------------------------------\n";
cout<<"Debugging crap..........\n";
cout<<" cipher["<<i<<"] = "<<cipher[i]<<endl;
cout<<" (int)cipher["<<i<<"] = "<<(int)cipher[i]<<endl;
cout<<" fillMe.at(int)(cipher["<<i<<"] = "<<fillMe.at( (int)cipher[i])<<endl;
fillMe.at( (int)cipher[i]) = fillMe.at( (int)cipher[i]) + 1;
}
catch( exception& e ) {
cout << "Exception raised: "<<e.what()<<endl;
}
}
freq = fillMe;
}
//*/ //END BLOCK COMMENT