Write a program that decodes secret messages read from a tex

Write a program that decodes secret messages read from a text file using an \"alphabetic shift.\" So, for example, a shift might appear as:


ORIGINAL: ABCDE...XYZ
SHIFTED: CDEFG...ZAB

for example, BEER with a shift of 2 would be DGGT.


You must utilize stepwise refinement to break this program into appropriate and less-complicated components, say:

1.) Read the encoded message from the attached MessageIn.txt file,
2.) Count the instances of each character in the file,
3.) The most common character in the message should be an \"E\". Use this information to calculate the amount of the shift,
4.) Print out the decoded message.

I have included Program.cs as an pics which demonstrates one way a character can be assigned an index value into an array.

uoing System; using System.Collections.Generici uaing system.Linq; uaing Syste.Texti uaing syste. Threading.Taakai uaing system. IO; amespace ConsoleApplicationi8 lass ReadFile private static StreamReader infile; private static ring instring: static void Main(atring[1 args) try infilenew StreamReader(\"H:/Visual Studio 2013/Projects/consoleApplicationi8/consoleApplication18/bin/Debug/Me ssageIn.txt\") instring = infile,ReadLine(); Console.writeLineinstring) ; infile.close catch (System. IO.IOException exc) Console WriceLine (\"ERROR!\" ReadFile(1) (1).cs Show all | ×

Solution

using System;
using System.IO;

class Program
{
/// <summary>
/// Apply Caesar cipher with shift.
/// </summary>
static string Encrypt(string value, int shift)
{
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
// Letter.
char letter = buffer[i];
// Add shift to all.
letter = (char)(letter + shift);
// Subtract 26 on overflow.
// Add 26 on underflow.
if (letter > \'z\')
{
letter = (char)(letter -3);
}
else if (letter < \'a\')
{
letter = (char)(letter + 3);
}
// Store.
buffer[i] = letter;
}
return new string(buffer);
}

static void Main()
{
using (StreamReader sr = new StreamReader(@\"E:\\MyTextFile.txt\"))
{
String line;

while ((line = sr.ReadLine()) != null)
{
  
string a = sr.ReadLine();
string b = Encrypt(a, 2); // Ok
Console.WriteLine(\"Original String \"+a);
Console.WriteLine(\"Encrypted String \"+b);

}
}

  
  
  
}
}

Output:

Original String bee

Encrypted String dgg


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site