So I'm currently working on a program that does the following tasks:

Read the encoded message from the attached MessageIn.txt file using StreamReader
Count the instance of each character in the file,
The most common character in the message should be an "E". Use this information to calculate the amount of the shift,
Print out the decoded message.

I've got the whole importing part but i'm having a lot of problems figuring out how to take the contents of the text file and put it into an array, then using that array to determine the most common character that appears.

Here's what i've got so far to import the text file:

    private static StreamReader inFile; 

    static void Main(string[] args)
    {
        string inValue;
       try
        {
            inFile = new StreamReader("MessageIn.txt"); //assumes the text file in the in bin Debug or Release folder
            while ((inValue = inFile.ReadLine()) != null)
            {


            }

        }
        catch (System.IO.IOException exc)
        {
            Console.WriteLine("ERROR");
        }


    }


    }

Someone please give me some guidance on where to go next. Much appreciated!

There's no 'E' in the this message.
Please explain how do you need to handle the message. What information you need to extract?
Explain the message layout so we can be of help.
Or you just want to convert the string to an char array?

Edited 4 Years Ago by AleMonteiro

The program needs to 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

Sorry forgot to add this bit to the opener. So yes there is no E in the message so each character needs to be entered into an array so it can be determined which one shows up the most. Whichever is the most frequent(S is the most frequent) is actually E "encoded".

That make sense?

The easiest way to get all the text into an array is File.ReadAllLines()

String[] lines = File.ReadAllLines("MyFileNameHere.txt");

Once you have all the text, you loop through the array, then loop through each character in the string. But you don't really care about line breaks and stuff, so just use File.ReadAllText()

String allTheText = File.ReadAllText("MyFileNameHere.txt");

Edited 4 Years Ago by Momerath

This article has been dead for over six months. Start a new discussion instead.