C++为一串单词添加空格

C++ adding spaces to a string of words

本文关键字:一串 单词 添加 空格 C++      更新时间:2023-10-16

例如,我有一个密文代码"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。因此,无法确定在哪里放置空格。也许是一个非常复杂的人工智能算法,它根据它看到的先前单词来确定下一个单词。