c++简单加密
C++ Simple Encryption
我读了Bjarne Stroustrup写的《c++编程语言》这本书,他的一个练习是做一个简单的加密。我输入一些东西,用std::cin读取它并加密它+将加密的文本打印到屏幕上。我是这样做的:
In my int main():std::string read;
std::cin >> read;
encript(read);
我的功能(只是一部分):
void encript(const std::string& r){
std::string new_r;
for(int i = 0; i <= r.length(); i++)
{
switch(r[i])
{
case 'a':
new_r += "a_";
break;
case 'b':
new_r += "b_";
break;
case 'c':
new_r += "c_";
break;
case 'd':
new_r += "d_";
break;
... //goes on this way
}
}
std::cout << new_r << std::endl;
我现在的问题是我真的要写每一个字符吗?我的意思是这些只是非大写字符。也有特殊字符,数字等。
还有别的方法吗?
当然还有另一种方法:
new_r += std::string(1,r[i]) + "_";
如果使用range-for,会更简洁:
std::string new_r;
for (char c : r) {
new_r += c;
new_r += '_';
}
这是相同的:
void encript(const std::string& r){
std::string new_r;
for(int i = 0; i < r.length(); i++) // change this too
{
new_r += r[i];
new_r += "_";
}
std::cout << new_r << std::endl;
或者您可以直接使用STL。不必使用c++ 11:
string sentence = "Something in the way she moves...";
istringstream iss(sentence);
ostringstream oss;
copy(istream_iterator<char>(iss),
istream_iterator<char>(),
ostream_iterator<char> (oss,"_"));
cout<<oss.str();
输出:S_o_m_e_t_h_i_n_g_i_n_t_h_e_w_a_y_s_h_e_m_o_v_e_s_。_. _. _
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 简单加密算术库(SEAL)和SEAL::Ciphertext变量
- 简单仿射密码加密解密
- 简单加密和解密 TXT 文件 [C++]
- 使用加密密钥进行简单加密
- 简单的C++字符数组加密功能 - 段故障
- 简单C++加密 - 解密库
- 将简单的 C 加密转换为C++
- 简单加密程序集程序-写入内存位置时发生访问冲突
- C++简单加密
- 加密文本文件的简单方法
- 学校作业的简单密码加密
- UTF-8 字符串的简单加密,其中结果为以 NULL 结尾的字符串
- 通过为每个字母分配(新)值在简单加密中使用数组
- C++中的简单文本文件加密
- c++中的简单加密/解密
- Boost asio SSL简单加密程序
- c++简单加密
- 在c++中实现公钥加密的简单方法
- 从一个简单的c++加密文件中读取