HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Creating the start of a Caesar cipher

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecreatingcaesarstartcipher

Problem

My code takes an input, uses the input to take a number of letters from the start of the alphabet and put them at the end. I intend to use this to create a Caesar style cipher.

Is there a way to do this more elegantly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            GettingInput gi = new GettingInput();
            var input = gi.GetInput();
            Console.WriteLine("Your input:\n{0}", input);
            List alphabet = new List();

            for (char c = 'a'; c  0; i--)
            {
                alphabet.Remove(alphabet[i-1]);
            }

            for (int i = 0; i < 26; i++) 
            {
                Console.WriteLine(alphabet[i]);
            }

            Console.ReadLine();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class GettingInput
    {
        public int GetInput()
        {
            Console.WriteLine("Please input an int:");
            while (true)
            {
                try
                {
                    int input = Convert.ToInt32(Console.ReadLine());
                    return input;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.WriteLine("Please input a valid int:");
                }

            }

        }

    }
}


All feedback welcome!

Solution


  • Instead of create the whole list and modify it subsequently, you could calculate the value directly.



The code could like that:

static void Main()
{
    GettingInput gi = new GettingInput();
    var input = gi.GetInput();
    Console.WriteLine("Your input:\n{0}", input);

    var first = (int)'a';
    var last = (int)'z';
    var length = last - first;

    var query = Enumerable
        .Range(0, length)
        .Select(i => (char)(first + ((input + i) % length)));

    foreach (var number in query)
        Console.WriteLine(number);

    Console.ReadLine();
}


  • You could use int.TryParse(Console.ReadLine(), out input) instead of catching the exception

Code Snippets

static void Main()
{
    GettingInput gi = new GettingInput();
    var input = gi.GetInput();
    Console.WriteLine("Your input:\n{0}", input);

    var first = (int)'a';
    var last = (int)'z';
    var length = last - first;

    var query = Enumerable
        .Range(0, length)
        .Select(i => (char)(first + ((input + i) % length)));

    foreach (var number in query)
        Console.WriteLine(number);

    Console.ReadLine();
}

Context

StackExchange Code Review Q#134966, answer score: 3

Revisions (0)

No revisions yet.