我如何增加c ++中的字母? [英] How do i increment letters in c++?

查看:273
本文介绍了我如何增加c ++中的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a Caesar Cipher in c++ and i can't figure out how to increment a letter.

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

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'.

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


推荐答案

letter 是一个 char ,而不是一个 char 也不是字符串。

This snippet should get you started. letter is a char and not an array of chars nor a string.

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'!)和祝你好运!

Watch out when you get to 'z' (or 'Z'!) and good luck!

这篇关于我如何增加c ++中的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆