1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<string>
#include<fstream>
#include<cstring>
using namespace std;
// NOTE: This function will only work for lowercase letters.
inline int charToIndex(const char character)
{
// ASCII 'a' is 97 in decimal.
return static_cast<int>(character - 97);
}
// Pass by reference to const. I don't see you changing the string.
int decrypt(const string& word)
{
// You can get the length value from the string itself once (to avoid calling
// this function over and over).
int wordLength = static_cast<int>(word.length());
// With charToIndex() you can use arrays to hold the values more naturally.
// Access an index like this: place[ charToIndex('a') ]
int freq[26] = {0};
int letterCount[26] = {0};
int place[26] = {0};
// I didn't get far enough to see what this was for.
char commoneng[26] = {'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd', 'c', 'u', 'm', 'f', 'p', 'g', 'w', 'y', 'b', 'v', 'k', 'x', 'j', 'q', 'z'};
// This loop tells us how many of each letter we have.
for(int i = 0; i < wordLength; i++)
{
// Use the next letter to figure out the appropriate index, and then increment the value there.
letterCount[ charToIndex(word[i]) ]++;
}
}
| |