Print a rectangular frame around array values

Write a program to accept the list of strings and prints them, one per line, in a rectangular frame.

For example:

Input:
["Hello", "World"]

Output:
*********
* Hello *
* World *
*********

Solution:

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

namespace PrintPattern
{
    class Program
    {
        static void Main()
        {
            var words = new[] { "Hello", "World" };
            var length = words.Max(w => w.Length);

            var sb = new StringBuilder();

            var tabs = new string('*', length + 4);

            sb.AppendLine(tabs);
            foreach (var item in words)
            {
                sb.AppendFormat("* {0} *", item.PadRight(length));
                sb.AppendLine();
            }
            sb.AppendLine(tabs);

            Console.WriteLine(sb.ToString());
            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *