编写加密程序,将所有字母转化为该字母后面的三个字母,其他不转换

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(){
	int i,n;
	char str[30]="";
	gets(str);
	for(i=0;str[i]!='\0';i++)
	{
        if(str[i]>='a'&&str[i]<='z')
		{
			n=str[i]-'a';
			n=(n+3)%26;
			str[i]=n+'a';
		}
		else if(str[i]>='A'&&str[i]<='Z')
		{
			n=str[i]-'A';
			n=(n+3)%26;
			str[i]=n+'A';
		}
	}
	printf("%s",str);
	system("pause");
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以帮你编写这个程序。首先,我们来实现移位密码加密的函数: ```python def shift_encrypt(plaintext, key): ciphertext = "" for char in plaintext: if char.islower(): ciphertext += chr((ord(char) - 97 + key) % 26 + 65) else: ciphertext += char return ciphertext ``` 这个函数接受两个参数,明文和密钥,返回密文。它会遍历明文中的每个字符,如果是小写字母,则将其转换成大写字母后再进行移位加密,否则直接保留原字符加密的方式是将字符的 ASCII 码值减去 97(小写字母 a 的 ASCII 码值),加上密钥,再对 26 取模,最后加上 65(大写字母 A 的 ASCII 码值),转换成对应的大写字母。注意,这里我们假设明文中只包含小写字母和其他字符,如果有其他情况需要额外进行处理。 接下来,我们来实现文件读写的功能。这里我们使用 Python 中的文件对象,打开明文文件和密文文件,分别读取明文并加密,将密文写入到密文文件中: ```python def shift_file_encrypt(input_file, output_file, key): with open(input_file, "r") as f: plaintext = f.read() ciphertext = shift_encrypt(plaintext, key) with open(output_file, "w") as f: f.write(ciphertext) ``` 这个函数接受三个参数,明文文件名、密文文件名和密钥。它使用 `with` 语句打开文件,读取明文并调用上面的 `shift_encrypt` 函数加密,最后将密文写入到密文文件中。注意,这里我们使用的是文本模式,即将文件内容视为字符串处理,如果需要处理二进制文件需要使用二进制模式。 最后,我们可以编写一个测试函数,来测试我们的程序是否正常工作: ```python def test_shift_file_encrypt(): input_file = "plaintext.txt" output_file = "ciphertext.txt" key = 3 with open(input_file, "w") as f: f.write("hello world") shift_file_encrypt(input_file, output_file, key) with open(output_file, "r") as f: ciphertext = f.read() assert ciphertext == "KHOOR ZRUOG" print("All tests passed") ``` 这个函数先创建一个明文文件,然后调用我们的加密函数进行加密,最后读取密文文件并检查是否正确。如果一切正常,会输出 "All tests passed"。 完整代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞翔的鱼。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值