读入外部文件,然后在Caesar移位后显示
本文关键字:Caesar 显示 然后 外部 文件 | 更新日期: 2023-09-27 18:25:03
区块报价
我已经成功地从外部文件中读取并显示了我的代码。我还获得了一个简单的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);
}
}
您就快到了。查看下面代码段中的最后一个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));
}