如何在c ++中增加字母?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在c ++中增加字母?相关的知识,希望对你有一定的参考价值。
我正在用c ++创建一个Caesar Cipher,我无法弄清楚如何增加一个字母。
我需要每次将字母增加1并返回字母表中的下一个字母。像下面这样添加1到'a'
并返回'b'
。
char letter[] = "a";
cout << letter[0] +1;
这段代码应该让你开始。 letter
是一个char
,而不是一系列char
s也不是字符串。
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);
}
Output: b
还要添加环绕逻辑(这样当letter[0]
是z
时,你设置为a
而不是递增),并考虑大小写。
letter ++有用吗?总而言之,char是一种数字类型,所以它会增加ascii code。但我相信它必须定义为char letter
而不是数组。但要注意在'Z'中添加一个。你会得到'['= P
#include <iostream>
int main () {
char a = 'a';
a++;
std::cout << a;
}
这似乎运作良好;)
char letter = 'a';
cout << ++letter;
它有效,但不要忘记,如果你增加'z'你需要得到'a'所以也许你应该通过一个检查函数,当你得到'z'时输出'a'。
waleed @ waleed-P17SM-A:〜$ nano Good_morning_encryption.cpp waleed @ waleed-P17SM-A:〜$ g ++ Good_morning_encryption.cpp -o Good_morning_encryption.out waleed @ waleed-P17SM-A:〜$ ./Good_morning_encryption.out输入你的text:waleed加密文本:jnyrrq waleed @ 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:
" << text << endl;
//return 0 (because the the main function is an int so it must return an integer value
return 0;
}
注意:这称为caesar密码加密,它的工作原理如下:
ABCDEFGHIJKLMNOPQRSTUVWXYZ NOPQRSTUVWXYZABCDEFGHIJKLM所以例如我的名字是waleed它将写成:JNYRRQ所以它只需在每个字母上添加13个字母
我希望能帮助你
你可以使用'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
将字母[n]转换为byte *并将其引用值增加1。
以上是关于如何在c ++中增加字母?的主要内容,如果未能解决你的问题,请参考以下文章