string - 带数字的字符串转换
问题描述
shift 函数应该根据给定的数字来移动字符
function shift_char(c: Character; n:Integer) return Character is
I : Integer;
begin
if Is_Lower(c) then
I:= ((((Character'Pos(c) rem 26) + n) mod 26) + 97);
elsif Is_Upper(c) then
I:= ((((Character'Pos(c) rem 26) + n) mod 26) + 65);
end if;
return Character'val(I);
end;
解决方案
如此处所述,您首先需要将每个范围内的最低字符映射为零。代替rem 26
,减去初始字符的位置,如下面的变体所示:
function Shift_Char (C : Character; N : Integer) return Character is
Lower_A : constant Integer := Character'Pos (Lower'First);
Upper_A : constant Integer := Character'Pos (Upper'First);
I : Integer := Character'Pos (C);
begin
if Is_Lower (C) then
I := ((((I - Lower_A) + N) mod 26) + Lower_A);
end if;
if Is_Upper (C) then
I := ((((I - Upper_A) + N) mod 26) + Upper_A);
end if;
return Character'Val (I);
end Shift_Char;
安慰:
generated single_key is : 25
generated multi_keys list is : 16 8 25 11
before encryption with caesar: Ada Is Cool
encrypted:Zcz Hr Bnnk
decrypted: Ada Is Cool
before encryption with vigenere: Ada Is Cool
encrypted: Qlz Ti Knzb
decrypted: Ada Is Cool
推荐阅读
- c++ - 试图用简单的方程创建函数
- c# - Xamarin.Android 保存当前进度
- javascript - RegEx 与 MT940 语句中的最后一行不匹配:86:标记
- android - Android Studio 3 USB调试离线手机
- spring - 在 Spring Boot 中更改单个控制器的 x-frame-options 标头
- php - 我可以使用 PHP 从谷歌分析中获取唯一用户数据吗
- bash - 循环和 grep 只为最后一行写入输出
- php - 为什么这里的地图反馈 API 会抛出验证异常
- qt - 是什么导致上下文菜单关闭?
- haskell - 如何对列表中的数字求和