如何在c++中增加字母
How do I increment letters in c++?
我正在用c++创建凯撒密码,我不知道如何增加一个字母。
我需要每次将字母增加1,并返回字母表中的下一个字母。如以下所示,将'a'
加1并返回'b'
。
char letter[] = "a";
cout << letter[0] +1;
这段代码应该可以让您入门。letter
是char
,不是char
的数组,也不是字符串。
static_cast
确保'a' + 1
的结果被视为char
。
> cat caesar.cpp
#include <iostream>
int main()
{
char letter = 'a';
std::cout << static_cast<char>(letter + 1) << std::endl;
}
> g++ caesar.cpp -o caesar
> ./caesar
b
当你到达'z'
(或'Z'
!)时要小心,祝你好运!
它按原样工作,但由于添加了将表达式提升为int
,因此您希望再次将其强制转换回char
,以便您的IOStream将其呈现为字符而不是数字:
int main() {
char letter[] = "a";
cout << static_cast<char>(letter[0] + 1);
}
输出:b
还添加环绕逻辑(这样当letter[0]
是z
时,您将设置为a
而不是递增),并考虑大小写
您可以使用 a +((信- a + n) % 26); 假设在z之后还需要a。"z"+ 1 = ' a '
#include <iostream>
using namespace std;
int main()
{
char letter='z';
cout<<(char)('a' + ((letter - 'a' + 1) % 26));
return 0;
}
看这个https://stackoverflow.com/a/6171969/8511215
字母++管用吗?All in All char是数字类型,因此它将增加ascii码。但我相信它必须被定义为char letter
而不是数组。但是要注意不要在"Z"后面加1。您将得到'[' =P
#include <iostream>
int main () {
char a = 'a';
a++;
std::cout << a;
}
这似乎很好;)
char letter = 'a';
cout << ++letter;
waleed@waleed-P17SM-A:~$ nano Good_morning_encryption.cppwaleed@waleed-P17SM-A:~$ g++ Good_morning_encryption.cpp -o Good_morning_encryption.outwaleed@waleed-P17SM-A: ~ $。/Good_morning_encryption.out输入你的文本:waleed加密文本:jnyrrqwaleed@waleed-P17SM-A:~$ cat Good_morning_encryption.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
//the string that holds the user input
string text;
//x for the first counter than makes it keeps looping until it encrypts the user input
//len holds the value (int) of the length of the user input ( including spaces)
int x, len;
//simple console output
cout << "Enter your text:";
//gets the user input ( including spaces and saves it to the variable text
getline(cin, text);
//give the variable len the value of the user input length
len = (int)text.length();
//counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than len
for(x = 0; x < len; x++) {
//checks each letts (and spaces) in the user input (x is the number of the offset keep in mind that it starts from 0 and for example text[x] if the user input was waleed would be w since its text[0]
if (isalpha(text[x])) {
//converts each letter to small letter ( even though it can be done another way by making the check like this if (text[x] =='z' || text[x] == 'Z')
text[x] = tolower(text[x]);
//another counter that loops 13 times
for (int counter = 0; counter < 13; counter++) {
//it checks if the letts text[x] is z and if it is it will make it a
if (text[x] == 'z') {
text[x] = 'a';
}
//if its not z it will keeps increamenting (using the loop 13 times)
else {
text[x]++;
}
}
}
}
//prints out the final value of text
cout << "Encrypted text:n" << text << endl;
//return 0 (because the the main function is an int so it must return an integer value
return 0;
}
注意:这被称为凯撒密码加密,它的工作原理是这样的:
ABCDEFGHIJKLMNOPQRSTUVWXYZNOPQRSTUVWXYZABCDEFGHIJKLM举个例子,我的名字是瓦利德写为:JNYRRQ所以它只是给每个字母加上13个字母
希望对你有所帮助
它工作,但不要忘记,如果你增加'z',你需要得到'a',所以也许你应该通过一个检查函数,输出'a'当你得到'z'
将字母[n]转换为byte*,并将其引用值增加1
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 增加传递地址的值
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 将一对插入地图并增加计数?
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- C++:功能在输出前自动使用50减/增加输入数
- 追加位以增加字符的大小
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 内存增加和字符串流?
- 如何增加使用 CUDA 实现的 FLOPS