[C# Exercise] Write an application program to encrypt input strings. The encryption rules for alphabetic strings are as follows: 'a'→'d' 'b'→'e' 'w'→'z' ... 'x '→'a' 'y'→'b' 'z'→'c'

topic:

Write an application program to encrypt input strings. The encryption rules for alphabetic strings are as follows:
'a'→'d' 'b'→'e' 'w'→'z' ... 'x'→'a ' 'y'→'b' 'z'→'c' 'A'→'D' '
B'→'E' 'W'→'Z' ... 'X'→'A' 'Y'→' B' 'Z'→'C'?
For other characters, no encryption is performed.

I heard about this topic a long time ago, but I didn’t know how to do it. It’s different now.

According to the alphabetical order a, b, c, d, e, f, g..., each encrypted string is the original string +3 in the alphabet;

So how about +3?

Here I thought of using the decimal system of the ASCII code, and each string has a position in the ASCII code,

So write the code:

Store the input string into a variable;

string str = Console.ReadLine();

Then use foreach to traverse each character in the string;

foreach (var item in str)

Judgment, if item >= 'a' and item <= 'w' (same for uppercase), process it,

Store the encrypted integer characters in num, add 3 and use char to display the converted output;

if (item >= 'a' && item <= 'w' || item >= 'A' && item <= 'W')
{
    int num = item;
    num += 3;
    Console.Write((char)num);
}

The last three letters of the judgment, if item >= 'x' and item <= 'z' (same for upper case), process it,

Store the encrypted integer characters in num, subtract 23 and use char to display the converted output;

else if (item >= 'x' && item <= 'z' || item >= 'X' && item <= 'Z')
{
    int num = item;
    num -= 23;
    Console.Write((char)num);
}

Look at the last sentence of the title again, for other characters, do not encrypt and output directly; 

else
{
    Console.Write(item);
}

Compilation and running results are as follows;

 

The source code is as follows:

string str = Console.ReadLine();
foreach (var item in str)
{
    if (item >= 'a' && item <= 'w' || item >= 'A' && item <= 'W')
    {
        int num = item;
        num += 3;
        Console.Write((char)num);
    }
    else if (item >= 'x' && item <= 'z' || item >= 'X' && item <= 'Z')
    {
        int num = item;
        num -= 23;
        Console.Write((char)num);
    }
    else
    {
        Console.Write(item);
    }
}

It's not easy to make, let's have a one-click three-link o(* ̄▽ ̄*)ブ

Guess you like

Origin blog.csdn.net/xichi_12396/article/details/119349680