Home > database >  How do I fix this encrypting program?
How do I fix this encrypting program?

Time:11-22

I have an encrypting program in C# I'm doing for a class, and just need a little help with something.

Whenever it's encrypting something that should be turned into a 'Z', it turns into an '@'. I'm not sure why it does this.

could anyone help out with this?

                char key;
                int shift = 0;
                string clearText = string.Empty;
                Console.WriteLine("Please input your encryption key in uppercase:");
                key = Convert.ToChar(Console.ReadLine());
                shift = key - 'A';
                // Console.WriteLine("shift = {0}", shift);
                Console.WriteLine("Please input your string in uppercase letters:");
                clearText = Console.ReadLine();
                foreach (char ch in clearText)
                {
                    if (Char.IsUpper(ch))
                    {
                        if (Convert.ToChar(ch   shift) < 'Z')
                            Console.Write(Convert.ToChar(ch   shift));
                        else Console.Write(Convert.ToChar(ch   shift - 26));
                    }
                    else if (ch == ' ')
                        Console.Write(' ');

CodePudding user response:

You've checked for characters up to 'Z', but not including Z.

if (Convert.ToChar(ch   shift) < 'Z') // Change this line ...
if (Convert.ToChar(ch   shift) <= 'Z') // ... to this.
  • Related