Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I fix this encrypting program?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                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(' ');

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading