【问题标题】:How do I increment letters in c++?如何在 C++ 中增加字母?
【发布时间】:2012-01-19 05:03:28
【问题描述】:

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

我需要每次将字母加 1 并返回字母表中的下一个字母。像下面这样将 1 添加到 'a' 并返回 'b'

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

【问题讨论】:

  • 你试过了吗?发生了什么?
  • 对不起,这只是一个简单的例子来强调我正在尝试做的事情,程序代码似乎有点无关紧要。
  • @Tomalak,这不是在 user1095463 编辑问题之前。
  • @user1095463:听起来有点像家庭作业?如果是,请标记为这样
  • @user1095463:对您而言似乎无关紧要的事情可能与我们无关;这就是为什么你是提问者,我们是回答者!

标签: c++ char encryption


【解决方案1】:

这个 sn-p 应该可以帮助您入门。 letterchar,而不是 chars 的数组,也不是字符串。

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'!)时要小心,祝你好运!

【讨论】:

  • 这依赖于实现定义的行为,因为 C 和 C++ 不保证字母字符是连续的。
  • ASCII 和 Unicode 标准肯定涵盖了这一点吗?
  • 是的,确实如此,但 C 被设计为可移植的——这意味着它可以在可能没有幸使用 ASCII 或 Unicode 的计算机上运行。
  • @Johnsyweb - Saagar Jha 是正确的。有现实世界的标准化字符集,以及使用它们的 C++ 实现。使用这样的实现,您的解决方案将不起作用。对于愿意假设 ASCII 或 Unicode 的人,您的解决方案很好,但是当移植到另一个系统。
【解决方案2】:

它按原样工作,但由于添加将表达式提升为 int,您希望再次将其转换回 char,以便您的 IOStream 将其呈现为字符而不是数字:

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

Output: b

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

【讨论】:

    【解决方案3】:

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

    看到这个https://stackoverflow.com/a/6171969/8511215

    【讨论】:

      【解决方案4】:

      字母++有用吗? 总而言之,char 是一个数字类型,所以它会增加ascii code。 但我相信它必须定义为 char letter 而不是数组。但请注意不要在“Z”中添加一个。你会得到 '[' =P

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

      这似乎运作良好;)

      【讨论】:

        【解决方案5】:

        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 加密文本: 关注 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;
        
            }
        

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

        ABCDEFGHIJKLMNOPQRSTUVWXYZ NOPQRSTUVWXYZABCDEFGHIJKLM 所以例如我的名字是waaleed 它将被写为:JNYRRQ 所以它只需在每个字母中添加 13 个字母

        希望对你有帮助

        【讨论】:

        • Caesars Chiffre 是 +3 而不是 +13 顺便说一句。
        【解决方案6】:
        char letter = 'a'; 
        cout << ++letter;
        

        【讨论】:

        • 这只是输出a,然后递增letter
        • 是的,修复它。 (-1 这有点苛刻,不是吗?)
        • 我想删除我的 -1 但它现在已锁定,除非编辑答案:-(
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          将 letter[n] 转换为 byte* 并将其引用值增加 1。

          【讨论】:

          • 为什么要在递增之前投射?
          • C 或 C++ 中没有这种类型...您现在正在考虑 Java 或 C#。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-19
          • 2013-06-24
          • 1970-01-01
          • 2014-01-22
          • 2019-02-27
          • 2016-03-02
          • 1970-01-01
          相关资源
          最近更新 更多