如何在c ++中增加字母?

时间:2011-12-13 10:00:53

标签: c++ char encryption

我正在用c ++创建一个Caesar Cipher,我无法弄清楚如何增加一个字母。

我需要每次将字母增加1并返回字母表中的下一个字母。类似于以下内容,将1添加到'a'并返回'b'

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

8 个答案:

答案 0 :(得分:6)

这段代码应该让你入门。 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'时),请注意!祝你好运!

答案 1 :(得分:4)

它按原样运行,但由于添加会将表达式提升为int,因此您希望将其再次强制转换回char,以便您的IOStream将其呈现为字符而不是数字:< / p>

int main() {
   char letter[] = "a";
   cout << static_cast<char>(letter[0] + 1);
}

Outputb

同时添加环绕逻辑(这样当letter[0]z时,设置为a而不是递增),并考虑大小写。

答案 2 :(得分:2)

letter ++有效吗? 总而言之,char是一种数字类型,因此它会增加ascii code。 但我认为必须将其定义为char letter而不是数组。但要注意在'Z'中添加一个。你会得到'['= P

#include <iostream>

int main () {
    char a = 'a';
    a++;
    std::cout << a;
}

这似乎运作良好;)

答案 3 :(得分:1)

char letter = 'a'; 
cout << ++letter;

答案 4 :(得分:1)

它有效,但不要忘记,如果你增加'z'你需要得到'a'所以也许你应该通过一个检查函数,当你得到'z'时输出'a'。

答案 5 :(得分:1)

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 输入你的文字: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;

    }

注意:这称为caeser密码加密,它的工作原理如下:

ABCDEFGHIJKLMNOPQRSTUVWXYZ NOPQRSTUVWXYZABCDEFGHIJKLM 所以例如我的名字是waleed 它将写成:JNYRRQ 所以它只需在每个字母上添加13个字母

我希望能帮到你

答案 6 :(得分:1)

您可以使用'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

答案 7 :(得分:-1)

将字母[n]强制转换为byte *并将其引用值增加1.