String 带数字的字符串转换

String 带数字的字符串转换,string,ada,String,Ada,移位函数应根据给定的数字移位字符 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

移位函数应根据给定的数字移位字符

   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

哦,我明白我的错了谢谢!你的解决方案是完美的!