Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Caesar
{
    class Program
    {
        static void Main(string[] args)
        {
            //Reads text from file I am using absolute links as I am unsure on how to use relative links.
            string Text = System.IO.File.ReadAllText(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\caesarShiftEncodedText.txt");
            //iterates through 26 different caesar shift to find the correct shift by using the console.writeline and two variables the decrypt function below the text variable and the alphabet array.

            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));
                
            }
            // leaving the the user to enter any letter to end the program so we can view the results.
            Console.ReadLine();
            
        }
        // method to apply during the loop. has two variables caesarString the actual string variable and and int that shifts the alphabet to the right.
        public static string Decrypt(string caeserString, int move)
        {

            var ab =
                //numbers both capital and normal letters up to 26 using the int move.
                Enumerable
                .Concat(
                                    Enumerable.Range((int)'a', 26),
                        Enumerable.Range((int)'A', 26))
                .Select(i => (char)i)
                .ToArray();
            //stores the characters into a array

            var map =
                ab
                //maps the array.
                    .Zip(
                        ab.Concat(ab).Concat(ab).Skip(ab.Length + move),
                        (c0, c1) => new { c0, c1 })
                    .ToDictionary(x => x.c0, x => x.c1);
                    //returns and formats the string to the main method.
                        return String.Join("", caeserString.Select(x => map.ContainsKey(x) ? map[x] : x));
        }
    }
}
Posted
Updated 8-Dec-15 14:00pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Dec-15 17:05pm    
How can we discuss something, if you don't have a single method, not counting the entry point, of course? If you have some abstraction, such as implementation of the cipher, you need to have some distinct artifact, some type or at least a function. Define such function and document its ins and out and describe what it should do. Also, your code is badly spoiled by "magic constants" 26 and 27. This is not supportable. Try to make them variables, part of input data, otherwise make it explicitly defined function, give it a name which will explain what it does.

After you do it, explain what's your problem.

—SA
DanielBrownAU 8-Dec-15 18:26pm    
A helpful tip to try and track it down is to attach and the the debugger, step thru the code and look at what is going on and what the variables are.

This will allow you to inspect what going on.



Give a man a fish vs teach a man to fish....
PIEBALDconsult 8-Dec-15 19:05pm    
That, sir, is an excellent reason to avoid Linq. It's a good thing I'm already drinking.
BillWoodruff 9-Dec-15 5:04am    
First, get this code into a form where it will compile. Then, think about the strategy required here: consider that the same routine that Caeser encodes a string using an offset, could also decode the encoded string by using the offset negated.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Solved

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Caesar
{
    class Program
    {
        static void Main(string[] args)
        {
            //Reads text from file I am using absolute links as I am unsure on how to use relative links.
            string Text = System.IO.File.ReadAllText(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\caesarShiftEncodedText.txt");
           
            FileStream ostrm;
            StreamWriter writer;
            TextWriter oldOut = Console.Out;
            try
            {
                ostrm = new FileStream(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\Shifted.txt", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(ostrm);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open Redirect.txt for writing");
                Console.WriteLine(e.Message);
                return;
            }
            Console.SetOut(writer);
            //iterates through 26 different caesar shift to find the correct shift by using the console.writeline and two variables the decrypt function below the text variable and the alphabet array.
            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));
                
            }
            Console.SetOut(oldOut);
            writer.Close();
            ostrm.Close();
            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));

            }
            Console.WriteLine("Done");
            // leaving the the user to enter any letter to end the program so we can view the results.
            Console.ReadLine();
            
        }
        // method to apply during the loop. has two variables caesarString the actual string variable and and int that shifts the alphabet to the right.
        public static string Decrypt(string caeserString, int move)
        {

            var ab =
                //numbers both capital and normal letters up to 26 using the int move.
                Enumerable
                .Concat(
                                    Enumerable.Range((int)'a', 26),
                        Enumerable.Range((int)'A', 26))
                .Select(i => (char)i)
                .ToArray();
            //stores the characters into a array

            var map =
                ab
                //maps the array.
                    .Zip(
                        ab.Concat(ab).Concat(ab).Skip(ab.Length + move),
                        (c0, c1) => new { c0, c1 })
                    .ToDictionary(x => x.c0, x => x.c1);
                    //returns and formats the string to the main method.
                        return String.Join("", caeserString.Select(x => map.ContainsKey(x) ? map[x] : x));
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900