题目:

编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:
‘a’→’d’ ‘b’→’e’ ‘w’→’z’ …… ‘x’→’a’ ‘y’→’b’ ‘z’→’c’‘A’→’D’
‘B’→’E’ ‘W’→’Z’ …… ‘X’→’A’ ‘Y’→’B’ ‘Z’→’C’?
对于其他字符,不进行加密。

这道题目很久以前听说过,但是没咋会,现在看看倒是不一样了

根据字母顺序a、b、c、d、e、f、g…来看,每个加密后的字符串都是原来字符串在字母表里+3的;

那么怎么+3呢?

这里想到了用ASCII码的十进制来做,每个字符串都在ASCII码中有位置,

所以进行编写代码:

将输进的字符串存进变量里;
string str = Console.ReadLine();
接着用foreach遍历字符串里的每个字符;
foreach (var item in str)
判定,如果 item >= 'a' 并且 item <= 'w'时(大写同理),进行处理,

将加密后的整形字符,用num存起来,增加3之后用char显示转换后输出;
if (item >= 'a' && item <= 'w' || item >= 'A' && item <= 'W') { int num =
item; num += 3; Console.Write((char)num); }
判定后三个字母,如果 item >= 'x' 并且 item <= 'z'时(大写同理),进行处理,

将加密后的整形字符,用num存起来,减去23之后用char显示转换后输出;
else if (item >= 'x' && item <= 'z' || item >= 'X' && item <= 'Z') { int num =
item; num -= 23; Console.Write((char)num); }
再看题目最后一句,对于其他字符,不进行加密,直接输出; 
else { Console.Write(item); }
编译运行结果如下;

 

源代码如下:
string str = Console.ReadLine(); foreach (var item in str) { if (item >= 'a'
&& item <= 'w' || item >= 'A' && item <= 'W') { int num = item; num += 3;
Console.Write((char)num); } else if (item >= 'x' && item <= 'z' || item >= 'X'
&& item <= 'Z') { int num = item; num -= 23; Console.Write((char)num); } else {
Console.Write(item); } }
制作不易,来个一键三连呀o(* ̄▽ ̄*)ブ

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信