4. There is a line of text, which has been translated into a code according to the following rules: A-->Z B-->Y C-->X a-->z b-->y c-->x...

4. There is a line of text, which has been translated into a code according to the following rules

Topic
Insert picture description here
1. C language code

#include<stdio.h>

void Password(char ch)
{
    
    
	char arr[27];//保存26个小写字母
	char brr[27];//保存26个大写字母
	char a = 'a';
	char b = 'A';
	for (int i = 1; i < 27; i++)//小写字母赋值
	{
    
    
		arr[i] = a;
		a++;
	}
	for (int i = 1; i < 27; i++)//大写字母赋值
	{
    
    
		brr[i] = b;
		b++;
	}
	if (ch >= 'a' && ch <= 'z')//判断传进的字符是否为小写字母,若是进入循环
	{
    
    
		for (int i = 1; i < 27; i++)//遍历小写字母
		{
    
    
			if (ch == arr[i])//如果传入字符和小写字母相同就加密
			{
    
    
				ch = arr[26 - i + 1 ];//加密
				printf("%c",ch);
				break;//跳出一层循环
			}
		}
	}
	else if (ch >= 'A' && ch <= 'Z')//判断传进的字符是否为大写字母,若是进入循环
	{
    
    
		for (int i = 1; i < 27; i++)//遍历大写字母
		{
    
    
			if (ch == brr[i])//如果传入字符和大写字母相同就加密
			{
    
    
				ch = brr[26 - i + 1];//加密
				printf("%c", ch);
				break;//跳出一层循环
			}

		}
	}
	else//其他字符原样输出
	{
    
    
		printf("%c", ch);
	}
	
}

int main()
{
    
    
	char ch;
	while ((ch = getchar()) != '\n')//当键入回车时,停止输入字符串
	{
    
    
		Password(ch);
	}
	
	return 0;
}

2. Execution results
Insert picture description here

Day4 2020-12-14 Monday light snow

If you are a tear in my eyes, I will never cry. If you are a tear in my eyes, I will never cry
.
End

Guess you like

Origin blog.csdn.net/xiaoxiaoguailou/article/details/111148942