C++实验:凯撒加密

恺撒加密法加密规则:

将原来的小写字母用字母表中其后面的第3个字母的大写形式来替换,大写字母按同样规则用小写字母替换,对于字母表中最后的三个字母,可将字母表看成是首未衔接的。如字母c就用F来替换,字母y用B来替换,而字母Z用c代替。编程实现以下功能:输入一个字符串,将其加密后输出。

#include<iostream>
using namespace std;
int main()
{
	char str[100], code[100];
	cin >> str;
	int i = 0;
	while (str[i] != '\0')
	{
		if (str[i] >= 'A' && str[i] <= 'Z')

		{
			code[i] = 'a' + (str[i] - 'A' + 3) % 26;
		}

		else if (str[i] >= 'a' && str[i] <= 'z')

		{
			code[i] = 'A' + (str[i] - 'a' + 3) % 26;
		}
		i++;
	}
	cout << code;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fly_wt/article/details/79852087
今日推荐