在C++中移动角色
Shifting characters in C++
我尝试编写一个函数来执行简单的字符移动(向左或向右移动,具体取决于以下函数中的 shift 参数)。大写字母仍然是大写字母。这是我的方法:
char encodeCaesarCipherChar(char ch, int shift)
{
char result;
if (!isalpha(ch)) return ch;
result = ch + shift;
if (islower(ch) && result < 'a') {
result += int('z') - 1;
result -= int('a');
} else if (islower(ch) && result > 'z') {
result -= int('z');
result += int('a') - 1;
} else if (isupper(ch) && result < 'A') {
result += int('Z') - 1;
result -= int('A');
} else if (isupper(ch) && result > 'Z') {
result -= int('Z');
result += int('A') - 1;
}
return result;
}
当输入字符为"s"及以后时,此函数将停止正常工作。谁能指出我的方法有什么问题?
提前谢谢。
's' + 13 将溢出有符号char
。将结果保留在int
中,并在调整数字后和返回之前投射到char
。
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 具有文本角色扮演功能的移动系统
- 在C++中,我的主机游戏角色在游戏场数组内移动时不会停止
- 如何停止角色在控制台中移动
- 如何使角色在C 中移动
- 在标高/房间之间移动角色
- 在C++中移动角色
- 2d角色只能朝一个方向移动