我如何在 C++ 中增加字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8487255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i increment letters in c++?
提问by Marobri
I'm creating a Caesar Cipher in c++ and i can't figure out how to increment a letter.
我正在用 C++ 创建一个凯撒密码,我不知道如何增加一个字母。
I need to increment the letter by 1 each time and return the next letter in the alphabet. Something like the following to add 1 to 'a'
and return 'b'
.
我需要每次将字母加 1 并返回字母表中的下一个字母。类似于以下的内容将 1 添加到'a'
并返回'b'
.
char letter[] = "a";
cout << letter[0] +1;
采纳答案by Johnsyweb
This snippet should get you started. letter
is a char
and not an array of char
s nor a string.
这个片段应该让你开始。letter
是一个char
而不是一个char
s数组,也不是一个字符串。
The static_cast
ensures the result of 'a' + 1
is treated as a 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
Watch out when you get to 'z'
(or 'Z'
!) and good luck!
当你到达'z'
(或'Z'
!)时要小心,祝你好运!
回答by Lightness Races in Orbit
It works as-is, but because the addition promotes the expression to int
you want to cast it back to char
again so that your IOStream renders it as a character rather than a number:
它按原样工作,但因为添加将表达式提升为int
您希望char
再次将其强制转换,以便您的 IOStream 将其呈现为字符而不是数字:
int main() {
char letter[] = "a";
cout << static_cast<char>(letter[0] + 1);
}
Output: b
输出:b
Also add wrap-around logic (so that when letter[0]
is z
, you set to a
rather than incrementing), and consider case.
还添加环绕逻辑(以便 when letter[0]
is z
,您设置为a
而不是递增),并考虑大小写。
回答by thim
Does letter++ work?
All in all char is a numeric type, so it will increment the ascii code.
But I believe it must be defined as char letter
not an array. But beware of adding one to 'Z'. You will get '[' =P
字母++有用吗?总而言之 char 是数字类型,因此它会增加ascii 代码。但我相信它必须被定义为char letter
不是一个数组。但要注意在“Z”上加一个。你会得到 '[' =P
#include <iostream>
int main () {
char a = 'a';
a++;
std::cout << a;
}
This seems to work well ;)
这似乎运作良好;)
回答by Roee Gavirel
char letter = 'a';
cout << ++letter;
回答by Kurt
It works but don't forget that if you increment 'z' you need to get 'a' so maybe you should pass by a check function that output 'a' when you get 'z'.
它有效,但不要忘记,如果你增加“z”,你需要得到“a”,所以也许你应该通过一个检查函数,当你得到“z”时输出“a”。
回答by Waleed Qutob
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 Enter your text:waleed Encrypted text: jnyrrq waleed@waleed-P17SM-A:~$ cat Good_morning_encryption.cpp
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:\n" << text << endl;
//return 0 (because the the main function is an int so it must return an integer value
return 0;
}
Note: this is called caeser cipher encryption it works like this :
注意:这称为 caeser 密码加密,其工作方式如下:
ABCDEFGHIJKLMNOPQRSTUVWXYZ NOPQRSTUVWXYZABCDEFGHIJKLM so for example my name is waleed it will be written as : JNYRRQ so its simply add 13 letters to each letter
ABCDEFGHIJKLMNOPQRSTUVWXYZ NOPQRSTUVWXYZABCDEFGHIJKLM 所以例如我的名字是 waleed 它将被写成:JNYRRQ 所以它只需在每个字母上添加 13 个字母
i hope that helped you
我希望对你有帮助
回答by Bimalendu
You can use 'a'+((letter - 'a'+n)%26);assuming after 'z' you need 'a' i.e. 'z'+1='a'
您可以使用'a'+((letter - '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;
}
回答by belgther
cast letter[n] to byte* and increase its referenced value by 1.
将 letter[n] 转换为 byte* 并将其引用值增加 1。