Solution: A simple challenge

For personal reasons I’ve not been very active in the last week or so. Therefore the solution I promised has been a bit late in coming.

Here is the “reference solution” for the simple challenge that I set last week. It is by no means the only solution, nor is it necessarily the best solution (depending on how you define “best”)

class Program
    private static void Main(string[] args)
        int width = Convert.ToInt32(Console.ReadLine());
        int height = Convert.ToInt32(Console.ReadLine());

        DrawBox(width, height);


    private static void DrawBox(int width, int height)
        // Work out the interior width and height (i.e. the width
        // and height of the space inside the box)
        int interiorWidth = width - 2;
        int interiorHeight = height - 2;

        // Work out what the top and bottom of the box should look like
        string topAndBottom = new string('*', width);

        // Work out what the interior rows should look like
        string interiorRow = string.Concat(
            "*", new string(' ', interiorWidth), "*", Environment.NewLine);

        // Work out the entire interior using the "trick" of defining
        // a string with a repeating character for as many rows as the
        // interior needs to be, then replacing each of those characters
        // with the pattern for a row of the interior.
        string interior = new string('-', interiorHeight);
        interior = interior.Replace("-", interiorRow);

        // Write the box to the console.
Technorati Tags: ,,,,

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s