2

I would like to preface this by saying it is for an assessment, so I don't want you to directly give me the answer, I would like you to point me in the right direction, slightly tweak what I have done or just tell me what I should look into doing.

I am trying to create a Caesar Cipher to decipher a text document we have been given. It needs to print all the possible shifts in the console and output the final one to a text document. I'm thinking of trying to add frequency analysis to this later to find the correct one, but I have some problems now that I need to sort out before I can do that.

This is what I have made so far:

using System;
using System.IO;
class cipher

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

    string output = "";
    int shift;
    bool userright = false;
    string cipher = File.ReadAllText("decryptme.txt");

    char[] decr = cipher.ToCharArray();

    do {

        Console.WriteLine("How many times would you like to shift? (Between 0 and 26)");
        shift = Convert.ToInt32(Console.ReadLine());
        if (shift > 26) {
            Console.WriteLine("Over the limit");
            userright = false;
        }
        if (shift < 0) {
            Console.WriteLine("Under the limit");
            userright = false;
        }
        if (shift <= 26 && shift >= 0)
        {
            userright = true;
        }
    } while (userright == false);


    for (int i = 0; i < decr.Length; i++)
    {
        {
        char character = decr[i];

        character = (char)(character + shift);

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

        if (character > 'Z')
            character = (char)(character - 26);

        else if (character < 'A')
            character = (char)(character + 26);


          output = output + character;
        }

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

    StreamWriter file = new StreamWriter("decryptedtext.txt");
    file.WriteLine(output);
    file.Close();

}

}

Right now it compiles and read the document but when it runs in the console it prints shift one as 1 letter from the encoded text, shift 2 as 2 letters from it, etc.

I have no idea what I have done wrong and any help would be greatly appreciated. I have also started thinking about ASCII values for letters but have no idea how to implement this.

Once again, please don't just give me the answer or I will not have learned anything from this - and I have been trying to crack this myself but had no luck.

Thanks.

  • `foreach (char c in cipher)` is wrong - you can remove this entire `foreach-loop` - the `for` loop is enough. It's going to print the first letter `n` number of times (where `n` is the length of the original file). – Rob Dec 15 '15 at 12:11
  • You have a for loop within a for loop unnecessarily. I'm unsure what your logic was - perhaps it was an accident. Other than that it seems okay. Try it without the foreach and see what happens. – Michael Dec 15 '15 at 12:14
  • Thank you, Rob. This has started to improve it, but I now getting the encoded text printed to me 1 letter at a time (1 in shift 1, 2 in shift 2, etc.) all the way up to 751. – explainplease Dec 15 '15 at 12:16
  • Look at the code `output = output + character` for the reason for that. – steinar Dec 15 '15 at 12:19
  • Have a look at this question/answer on meta: http://meta.stackoverflow.com/a/254941/563088 Although I like the fact that you are asking for hints, this is not what this site is for. – Emond Dec 15 '15 at 12:22

1 Answers1

1

Break the problem down into smaller bite-sized chunks. Start by printing a single shifted line, say with a shift of 1.

When you have that part working correctly (and only then) extend your code to print 26 lines with shifts of 0, 1, 2, 3, ... 26. I am not sure if your instructor wants either or both of shift 0 at the start and shift 26 at the end. You will need to ask.

Again, get that working correctly, and write new code to analyse one line only, and give it some sort of score. Get that working properly.

Now calculate the scores for all the lines and pick out the line with the best score. That should be the right answer. If it isn't then you will need to check your scoring method.

Writing small incremental changes to a very simple starting program is usually a lot easier than trying to go straight from a blank screen to the full, complex, program. Add the complexity gradually, one piece at a time, testing as you go.

rossum
  • 15,344
  • 1
  • 24
  • 38