如何在c++中增加字母

How do I increment letters in c++?

本文关键字:增加 c++      更新时间:2023-10-16

我正在用c++创建凯撒密码,我不知道如何增加一个字母。

我需要每次将字母增加1,并返回字母表中的下一个字母。如以下所示,将'a'加1并返回'b'

char letter[] = "a";
cout << letter[0] +1;

这段代码应该可以让您入门。letterchar,不是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