使用C++中的字符矢量来编程凯撒密码
Using a vector of characters in C++ to program a Caesar Cipher
我一直在使用Caesar密码对消息进行加密。我尽量保持简单。
/*
* File: main.cpp
* Author: Skylar Croy
*
* Created on February 10, 2015, 11:50 AM
*
* Purpose: To encrypt a message using the Caesar Cipher
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
char letters[] =
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; // Make a vector, letter, which equals [a b c ... z]
int i, k; // i used in two loops, k used for shift 3 and mod calc
for (i=0; i<=25; i++) { // Run 26 times (1 for each letter)
k = (i + 3) % 26; // Performs mod arth. with shift of 3
letters[i+26] = letters[k]; // letter[i+26] = [d e f g ... c]
// Note that letters = [a b c ... z d e f g....c]
/* cout << letters[i]; // This code shows that the operation in this for loop is functioning correctly
cout << " ";
cout << letters[i+26];
cout << " n"; */
}
std:: string x; // declare string x
cin >> x;
int string_length = x.size();
for (i=0; i<=(string_length-1); i++){ // individual characters of string x can be referenced by x[0] etc
for (k=0; k<=25; k++){
if (x[i] == letters[k]) {
cout << letters[k+26];
}
}
}
return 0;
}
出于某种原因,当您尝试用w、x、y或z加密消息时,它不起作用,并打印随机垃圾。不确定出了什么问题。任何建议都会有帮助。谢谢
其他答案已经指出了您最初尝试中的错误。我只想说你的目标可以在两条线内实现:
for (auto& each : x) // where x is your std::string
each = (each - 'a' + 3) % 26 + 'a';
我还没有读完你的代码,但下面一行肯定超出了范围:
letters[i+26] = letters[k]; // letter[i+26] = [d e f g ... c]
您的letters
数组只有26长,但您正在访问第i+26
个元素。也许试试:
letters[(i+26)%26] = letters[k]; // letter[i+26] = [d e f g ... c]
您绝对应该NOT尝试分配超出数组范围的值!
同样,当你在中打印你的信件时
cout << letters[k+26];
您将超出数组的界限,这意味着您将打印垃圾。。。
这可能不是所有的错误,但这是一个开始。。。
还有一个提示(除了上面提到的在数组之外写入)-你不能只做这样的替换:
letters[i] = letters[k];
因为您将覆盖现有项目。也许更简单更好的选择是使用另一个数组来创建修改后的字母表?
char new_letters = new char[26];
for (int i=0; i<26; i++)
new_letters[i] = letters[(i+3) % 26];
并使用新阵列进行编码:
if ('a' - x[i] < 26)
cout << new_letters['a'-x[i]];
它将仅替换小写字母
@nonsensickle是正确的。只需为加密向量制作另一个长度为26的向量即可修复它。谢谢!
以下代码有效:
/*
* File: main.cpp
* Author: Skylar Croy
*
* Created on February 10, 2015, 11:50 AM
*
* Purpose: To encrypt a message using the Caesar Cipher
*/
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
char letters[] =
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; // Make a vector, letter, which equals [a b c ... z]
int i, k; // i used in two loops, k used for shift 3 and mod calc
char letters_cipher[25];
for (i=0; i<=25; i++) { // Run 26 times (1 for each letter)
k = (i + 3) % 26; // Performs mod arth. with shift of 3
letters_cipher[i] = letters[k]; // letter[i+26] = [d e f g ... c]
// Note that letters = [a b c ... z d e f g....c]
/*
cout << letters[i]; // This code shows that the operation in this for loop is functioning correctly
cout << " ";
cout << letters[i+26];
cout << " n";
*/
}
std:: string x; // declare string x
cin >> x;
int string_length = x.size();
for (i=0; i<=(string_length-1); i++){ // individual characters of string x can be referenced by x[0] etc
for (k=0; k<=25; k++){
if (x[i] == letters[k]) {
cout << letters_cipher[k];
}
}
}
return 0;
}
相关文章:
- 如何将凯撒密码的元素从字符串更改为字符串
- 如何在C++的凯撒密码程序中包含空格?
- 凯撒密码在C++中无法正确解密的问题
- 凯撒密码程序中"Debug Assertion Failed!"问题
- 凯撒轮班问题
- C++ "Program has stopped working" - ROT-107(凯撒密码)
- 在我的C++凯撒密码程序中出现错误
- 凯撒密码程序
- 凯撒密码删除数据
- C 凯撒加密项目(ASCII)
- C++简单的凯撒密码算法
- C++凯撒密码——理解ascii密钥
- 凯撒密码面向对象编程
- 使用C++中的字符矢量来编程凯撒密码
- 使用 C++ 实现凯撒密码时出错
- 凯撒密码 - 怎么了?
- 凯撒密码器/密码器程序.C++
- 凯撒密码,当字符为 >'z' 时环绕
- 凯撒密码与扭曲(神秘错误)
- 无法找出凯撒密码的问题C++