Substitution is an operation commonly used in the encryption process. You can design a simple replacement as follows: The uppercase letter A~z in alphabetical order into a circle, followed by the letter A after the letter A, for any one of the uppercase letters to be replaced, after the nth letter to replace it, that is, complete the replacement process. For example, when n=2, A is replaced by C, B is replaced by D, C is replaced by E, ..., y is replaced by Z, X is replaced by a, z is replaced by B. For lowercase letters, numbers can also be processed by analogy. Requires that you write a program that the user enters N (similar to a key) and replaces the user-entered string (similar to plaintext, composed of uppercase and lowercase letters) by letter to output (the output is similar to redaction). Think about how to make this program both complete the replacement (the encryption process) and complete the reverse replacement (decryption process).
#include <iostream.h>
#include <string.h>
void Main ()
{
Char str[100];
int n,c,i;
cout<< "Please enter a string:" <<endl;
CIN >> str;
cout<< "Please enter the key:" <<endl;
CIN >>n;
cout<< "If you need encryption, please enter 1" <<endl;
cout<< "If you need to decrypt, please enter 2" <<endl;
cin>>c;
if (c==1)
{
for (I=0;i<strlen (str); i++)
{
str[i]+= N;
if (str[i]> ' Z ' | | (str[i]> ' Z ' &&str[i]< ' a '))
str[i]-= (' z '-' a ') +1;
}
cout<<str<<endl;
}
else if (c==2)
{
for (I=0;i<strlen (str); i++)
{
str[i]-= N;
if (str[i]< ' A ' | | (str[i]> ' Z ' &&str[i]< ' a '))
str[i]+= (' z '-' a ') +1;
}
cout<<str<<endl;
}
}