此函数旨在从 char 数组中删除所有特殊字符、数字和空格。
// Michael E. Torres II
// Vigenere Cipher
// February 4, 2018
// C++ code to implement Vigenere Cipher
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <functional>
using namespace std;
// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
int x = (str[i] + key[i]) % 26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
// and returns the original text
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0; i < cipher_text.size(); i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] + 26) % 26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
transform(orig_text.begin(), orig_text.end(), orig_text.begin(), ::tolower);
}
return orig_text;
}
string removeNonAlpha(char *str)
{
unsigned long i = 0;
unsigned long j = 0;
char c;
while ((c = str[i++]) != ' ')
{
if (isalpha(c)) // this is where the breakpoint is automatically placed
{
str[j++] = c;
}
}
str[j] = ' ';
return str;
}
// Driver program to test the above function
int main(int argc, char *argv[])
{
string keyword = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
stringstream ss;
char a[] = "“I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right.” – Albert Einstein “Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.” – Albert Einstein";
int i = 0;
string str = removeNonAlpha(a);
str.append(512 - str.length(), 'X');
transform(str.begin(), str.end(), str.begin(), ::toupper);
transform(keyword.begin(), keyword.end(), keyword.begin(), ::toupper);
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
transform(cipher_text.begin(), cipher_text.end(), cipher_text.begin(), ::tolower);
transform(key.begin(), key.end(), key.begin(), ::tolower);
string orig = originalText(cipher_text, key);
cout << "Original/Decrypted Text : " << "n";
for (int i = 0; i < orig.size(); i += 81)
orig.insert(i, "n");
cout << orig;
cout << "nn" << "Ciphertext : " << "n";
for (int i = 0; i < cipher_text.size(); i += 81)
cipher_text.insert(i, "n");
cout << cipher_text;
cout << "nnPress ENTER key to Continuen";
getchar();
return 0;
}
char 数组可以很好地处理这个 while 循环,只要没有特殊字符 [.,%$@!^]。只要 char 数组中有任何特殊字符,它就会给我调试断言:
"程序:...\项目\控制台应用程序17\调试\控制台应用程序17.exeFile: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp行: 42
表达式: c>= -1 && c <= 255
。
程序"[11048] 控制台应用程序17.exe"已退出,代码为 3 (0x3(。
如果我在 repl.it 或 cpp.sh 上运行它,我不会遇到任何问题。我感谢任何帮助。谢谢。
它根本没有完成。它需要清理很多,但我只是想按原样测试它。
请参阅 https://msdn.microsoft.com/en-us/library/xt82b8z8.aspxISALPHA 期望一个介于 0 和 0xFF 之间的数字:
_isalpha_l如果 c 不是 EOF 或 介于 0 到 0xFF 之间(含 0 和 (。当调试 CRT 库 使用且 c 不是这些值之一,函数会引发 断言。
在传递给 isalpha 之前,您需要将字符转换为无符号字符。