C++为一串单词添加空格
C++ adding spaces to a string of words
例如,我有一个密文代码"KWSVVSYXKSBOKRKBNRKDKXNKNBEXUKBOKDKLKBGRO"
我对其进行了一些频率分析计算,并开始逆向工程来破解密码。
我的已解决密文是
"新解决的密码器文本与密钥是:百万富翁哈德和达醉卡区域塔巴当特"
现在唯一的问题是我不明白如何在单词之间正确放置空格,这样它就可以更英语"A millionaire a hard hat and drunk are..."
以下是我破解密码的代码
string cipher = "";
int i = 0, alphabet[26] = { 0 }, j, temp;
int n = cipher.length();
// declaring character array
char char_array[343];
// copying the contents of the
// string to char array
strcpy_s(char_array, cipher.c_str());
//print the entire cipher text and ASCII value
//for (int i = 0; i < n; i++)
//{
//cout << char_array[i] << endl;
// cout << "the ASCII value of " << char_array[i] << " is " << int(char_array[i]) << endl;
// }
//Find the most frequent letter in the cipher text
while (char_array[i] != ' ') {
if (char_array[i] >= 'A' && char_array[i] <= 'Z') {
j = char_array[i] - 'A';
++alphabet[j];
}
++i;
}
cout << "Frequency of all alphabets in the string is:" << endl;
for (i = 0; i < 26; i++)
cout << char(i + 'A') << " : " << alphabet[i] << endl;
//end most frequent
cout << endl;
const int N = sizeof(alphabet) / sizeof(int);
for (i = 0; i < 26; i++){
cout << "Most frequent letter is : " << char(distance(alphabet, max_element(alphabet, alphabet + N)) + 'A') << " trying key : " << distance(alphabet, max_element(alphabet, alphabet + N)) << endl;
//cout << alphabet[i] << "Most frequent position of key is trying: " << distance(alphabet, max_element(alphabet, alphabet + N)) << endl;
cout << "New solved cipherr text with key is : " << encrypt(char_array, distance(alphabet, max_element(alphabet, alphabet + N))) << endl << endl;
alphabet[distance(alphabet, max_element(alphabet, alphabet + N))] = 0;
}
string encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i = 0; i<text.length(); i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
result += char(int(text[i] + s - 65) % 26 + 65);
}
// Return the resulting string
return result;
}
我能想到的唯一解决方案是使用某种英语词典,遍历每个字符,直到它看到一个单词,然后在那里放一个空格。例如,此解决方案中的问题在于像 AND 这样的单词,它可以是 a 或 or and。因此,无法确定在哪里放置空格。也许是一个非常复杂的人工智能算法,它根据它看到的先前单词来确定下一个单词。
相关文章:
- C++为一串单词添加空格
- 为什么Visual Studio正在清除我的一串反斜杠
- 如何将内存中的位转换为一串和零?
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 在字符数组中存储和恢复一组单词
- 创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和
- C++ 如何对一串数字求平均值
- 从网络数据包复制一串 wchar 时访问冲突
- C - 将文本文件读为一串单词,然后将字符串分为向量
- 我如何在一串随机生成的字母I C 中查找一个单词
- 从一串数字C++创建一个数字序列
- 将一串十六进制存储到字符中
- 在另一个字符串中查找一串数字
- 从连续包含 3 个元音字母的一组单词中删除单词
- 将一串数字分解为26以下数字的计数方法
- 把一串单词解析成一组单词
- 将一串数字转换为整数向量 c++
- 将一串单词视为数组 (C++)
- 如何将数字"Bind"到一串单词/短语中,以便我可以循环调用它?
- 处理一长串单词.有可能同时把它分成几个部分吗