读入外部文件然后在凯撒转换后显示

时间:2015-11-30 20:34:57

标签: c# caesar-cipher

  

块引用

我已成功获取代码以从外部文件中读取文本并显示它。我也有一个简单的凯撒班的代码。我只是不知道如何让我的代码在文件中读取,然后执行Caesar转换并在凯撒转换后显示输出,有人可以帮忙。这是我的代码:

using System;
using System.IO; 

class Shift
{
    public static void Main(string [] args)

    {

        //Read in code to shift from text file.
        string file = @"txt"; //I have a file linked here
        string text = File.ReadAllText(file);
        //Show original unshifted code. 
        System.Console.WriteLine("Original text reads: \n\n{0}", text);

        //Show text after shift. 
        Console.WriteLine("The shifted code is: \n\n{1}", b);

    //Start Caesar Shift code as a seperate method.  
    }
    public static string Caesar(string value, int shift)
    {  
    char[] buffer = value.ToCharArray();
    for (int i = 0; i < buffer.Length; i++)
    {
        char letter = buffer[i];
        letter = (char)(letter + shift);
        if (letter > 'z')
        {
        letter = (char)(letter - 26);
        }
        else if (letter < 'a')
        {
        letter = (char)(letter + 26);
        }
        // Store shift. 
        buffer[i] = letter;
    }
    return new string(buffer);
  }
  //Add number to shift text. 
   public static void Second()
    {
    string a = text;
    string b = Caesar(a, 18); 
    }
}

1 个答案:

答案 0 :(得分:0)

你快到了。查看下面代码段中的最后一个Console.WriteLine。

public static void Main(string[] args)
{
    //Read in code to shift from text file.
    var file = @"txt"; //I have a file linked here
    var text = File.ReadAllText(file);
    //Show original unshifted code. 
    Console.WriteLine("Original text reads: \n\n{0}", text);

    //Show text after shift. 
    Console.WriteLine("The shifted code is: \n\n{0}", Caesar(text, 18));
}