PTA Experiment 7-3-5 Caesar Code (20 points)

Experiment 7-3-5 Caesar Cipher (20 points)
Portal
In order to prevent information from being easily stolen by others, it is necessary to convert the plain text of the electrical code into cipher text through encryption. Enter a character string (less than 80 characters) ending with a carriage return character, then enter an integer offset, encrypt it with the Caesar password and output it. The Caesar cipher is a simple replacement encryption technology. All letters in the plaintext are offset in the alphabet and then replaced with ciphertext. When the offset is greater than zero, it means a backward offset; when the offset is less than zero , Means forward shift.

Input format:
Input the first line to give a non-empty string (less than 80 characters) ending with a carriage return; input an integer offset in the second line.

Output format:
output the encrypted result string.

Input example 1:

Hello Hangzhou
2

Output sample 1:

Jgnnq Jcpibjqw

Input example 2:

a=x+y
-1

Output sample 2:

z=w+x

Solution ①ASCII code
Idea: ASCII code 65=A,90=Z. 97=a 122=z Friends who are
not familiar with ASCII code can use
printf("%d %d %d %d",'A','Z' ,'a','z'); get

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    
    
	//65=A,90=Z. 97=a 122=z 
	char ch[81];
	int of;  //offset
	gets(ch);scanf("%d",&of);
	of%=26;
	for(int i=0;i<strlen(ch);i++)
	{
    
    
		if( isalpha(ch[i])  )
		{
    
    
			if(  ch[i]>=65&&ch[i]<=90)
			{
    
    
				if( ch[i]+of>=65&&ch[i]+of<=90 ) ch[i]+=of;
				else if(ch[i]+of>90) ch[i]=ch[i]+of-26;
				else if(ch[i]+of<65) ch[i]=ch[i]+of+26;   
			}
			if(  ch[i]>=97&&ch[i]<=122)
			{
    
    
				if( ch[i]+of>=97&&ch[i]+of<=122 ) ch[i]+=of;
				else if(ch[i]+of>122) ch[i]=ch[i]+of-26;
				else if(ch[i]+of<97) ch[i]=ch[i]+of+26;   
			}	
		}
		printf("%c",ch[i]);
		
    }
}

Optimization: Use modular arithmetic optimization.
Considering -1%26=-1, which is not the desired 25, you can do this, (-1+26)%26=25%26=25;
at the same time, (26+25)% 26=25;
then the left shift and right shift can be expressed by the same formula, only need to add 26;
code:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    
    
	char ch[81];
	int of;
	gets(ch);scanf("%d",&of);
	of%=26;
	for(int i=0;i<strlen(ch);i++)
	{
    
    
		if( isalpha(ch[i])  )
		{
    
    
			if(  islower(ch[i]))
				ch[i]=('a'+ ( ch[i]-'a'+of+26 )%26  ) ;
			if(  isupper(ch[i]))
				ch[i]=('A'+ ( ch[i]-'A'+of+26  )%26 ) ;
		}
		printf("%c",ch[i]);
    }
}

Solution ②Construct the repeating interval. Is
n't it a variant of this problem? The
array elements are rotated to the right.
Idea: Construct a repeating interval
eg:
1, 2, 3,
which can be constructed as 1, 2, 3, 1, 2, 3, 1, 2 , 3.
Move left and right to see where you are at a glance.
Perfect to avoid the problem of negative modulus.
code:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
const char* lower="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
const char* upper="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
char r(char ch,int n)
{
    
    
	if( islower(ch))  return lower[ch-'a'+n+26];
	else if( isupper(ch))  return upper[ch-'A'+n+26];
	else return ch;
}
int main()
{
    
    	
	char ch[81];int of; //offset
	gets(ch);scanf("%d",&of);
	of%=26;
	for(int i=0;i<strlen(ch);i++)
	printf("%c",r(ch[i],of));
}

Guess you like

Origin blog.csdn.net/weixin_49640089/article/details/112712599