文字列を操作できるように大文字に変換しようとしていますが、自然な大文字の文字列を正常に操作でき、小文字を大文字に変換することもできますが、この変換方法を使用すると操作できません。
たとえば、暗号化で「hello」を渡すと、暗号化された文字列は「HELLO」になりますが、「HELLO」を (自然に大文字で) 渡すと、正しくシフトします。
使用する必要がある大文字を強制する別の方法はありますか、それとも何か間違っていますか?
int Caesar::encrypt (const std::string &message, std::string &emessage) {
int count = 0;
emessage = message;
std::transform(emessage.begin(), emessage.end(), emessage.begin(), ::toupper);
for (std::string::size_type i = 0; i < message.size(); i++) {
for (int j = 0; j < 26; j++) {
if (emessage[i] == std_alphabet[j]) {
std::replace(emessage.begin(), emessage.end(), message[i], c_alphabet[j]);
}
}
count++;
}
return count;
}
コンストラクタ:
Caesar::Caesar (int shift) {
// loop to populate vector with 26 letters of English alphabet
// using ASCII uppcase letter codes
for (int i = 0; i < 26; i++) {
std_alphabet.push_back(i + 65);
}
// fills Caesar alphabet with standard generated alphabet
c_alphabet = std_alphabet;
// shifts Caesar alphabet based off the constructor parameter
std::rotate(c_alphabet.begin(), c_alphabet.begin() + shift, c_alphabet.end());
}
テストファイル:
void testCaesar() {
Caesar test(4);
std::string original = "HELLO";
std::string encrypted = "";
test.encrypt(original,encrypted);
std::cout << encrypted << std::endl;
std::cout << original << std::endl;
}
int main() {
testCaesar();
return 0;
}
明らかにヘッダーとインクルードなどがありますが、それは基本的なコードです
ヘッダー ファイルには 2 つのプライベート ベクトルが含まれます