Caesar Cipher Shift(使用字母数组) [英] Caesar Cipher Shift (using alphabet array)

查看:22
本文介绍了Caesar Cipher Shift(使用字母数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用 C# 为我的作业编写 Caesar Cipher 程序,但遇到了问题.

I am currently writing a Caesar Cipher program in C# for my assignment and I am having a problem.

我正在使用一个数组来处理这个任务,我在其中存储了整个字母表,并声明了一个由数组中的字符索引定义的移位变量 - for 循环的迭代.移位计算在 foreach 循环中完成,该循环从从文本文件读取的字符串中获取字符.Foreach 循环包含在 for 循环中,该循环会迭代输出每个可能的移位.

I am approaching this task using an array where I store the whole alphabet and I declare a shift variable which is defined by character index in the array - the iteration of a for loop. The shift calculation is done in a foreach loop, that fetches a character from a string that is read from a text file. Foreach loop is contained within a for loop that iterates to output every possible shift.

然而,问题是当我尝试通过我的 shift 变量的值访问数组中的字符时,程序似乎没有访问我想要的字符,它只是输出与原始字符相同的字符细绳.

However, the problem is that when I try to access the character in an array by a value of my shift variable, the program doesn't seem to access the character I want, it just outputs the same character as in the original string.

这是程序的代码:

using System; 
using System.IO;

public class caesar_shift
{
    public static void Main()
    {
        string file = @"C:\Users\terasss2\Desktop\Programming and Data Structures\caesarShiftEncodedText.txt";      //String variable that stores a file location
        string encrypted_text = File.ReadAllText(file);     //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
        string decoded_text = " ";
        int shift = 0;
        char character = '0';

        char[] alphabet = new char[26]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

        Console.WriteLine("The encrypted text is \n{0}", encrypted_text);       //Display the encrypted text

        for(int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
        {
            foreach(char c in encrypted_text)
            {
                character = c;

                if(character == '\'' || character == ' ')
                    continue;

                shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable

                if(shift <= 0)
                    shift = shift + 26;

                if(shift >= 26)
                    shift = shift - 26;

                character = alphabet[shift];    //Set the character to a shifted letter by accessing the array element of a value shift

                Console.WriteLine(character);

                decoded_text = decoded_text + character; 
             }  

            Console.WriteLine("\nShift {0} \n {1}",i + 1, decoded_text);

         }
       }
}

推荐答案

我使用了您的代码.下面给出了解决方案,但你必须小心:你只能使用大写字母,因为上下图表是不同的.我使用了 ToUpper() 方法.对我来说很好用.我认为这就是你的问题所在.

I played a bit with your code. The following gives you the solution, but you have to take care: you couldonly use capital letters, because theres a difference in upper and lower charts. I used the ToUpper() method. Works fine for me. I think that's what your problem was.

public static void Main()
    {
        string encrypted_text = "BCD";     //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
        string decoded_text = " ";
        int shift = 0;
        char character = '0';
        encrypted_text = encrypted_text.ToUpper();

        char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        Console.WriteLine("The encrypted text is \n{0}", encrypted_text);       //Display the encrypted text

        for (int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
        {
            decoded_text = "";
            foreach (char c in encrypted_text)
            {
                character = c;

                if (character == '\'' || character == ' ')
                    continue;

                shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
                if (shift <= 0)
                    shift = shift + 26;

                if (shift >= 26)
                    shift = shift - 26;


                decoded_text += alphabet[shift];
            }
            Console.WriteLine("\nShift {0} \n {1}", i + 1, decoded_text);
        }
    }

这篇关于Caesar Cipher Shift(使用字母数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆