1

I have a collection of strings that the user can add to or subtract from. I need a way to print the strings out in columns so that the 1st letter of each string aligned. However I the number of columns must be changeable during run time. Although the default is 4 columns the use can opt for any number from 1 to 6. I have no idea how to format an unknown quantity of string into an unknown number of columns.

Example Input: it we so be a i o u t y z c yo bo go an

Example output of four columns

"Words" with 2 letters:

it so be we

yo bo go an

"Words" with 1 letter:

a i o u

t y z c

Note: not worried about parsing of the words I already have that in my code which I can add if helpful.

4
  • are you just trying to align the string letter by letter? If so why can't you just render the output in a fixed width font? Also, it would help to know what language you are using. Commented Nov 11, 2012 at 1:16
  • C# someone told me earier that if im going to put it as a tag it should be in the title. But I just need to align the 1st letter of each string Commented Nov 11, 2012 at 1:20
  • 1
    No. Never put the tags in the title. That's what the tags are for. Commented Nov 11, 2012 at 1:20
  • 1
    When you say columns, where are these columns? In an .xls? A grid? Table? Commented Nov 11, 2012 at 1:21

2 Answers 2

2

If you are trying to create fixed width columns, you can use string.PadLeft(paddingChar, width) and string.PadRight(paddingChar, width) when you are creating your rows.

http://msdn.microsoft.com/en-us/library/system.string.padleft.aspx

You can loop through your words and call .PadXXXX(width) on each word. It will automatically pad your words with the correct number of spaces to make your string the width you supplied.

Sign up to request clarification or add additional context in comments.

2 Comments

This is close to what I need but i need the words to start a new line once they hit the max number of columns.
@JamesThompson, try to call Environment.NewLine just before calling PadLeft or PadRight once hit max no of columns.
1

You can divide the total line width by the number of columns and pad each string to that length. You may also want to trim extra long strings. Here's an example that pads strings that are shorter than the column width and trims strings that are longer. You may want to tweak the behavior for longer strings:

    int Columns = 4;
    int LineLength = 80;

    public void WriteGroup(String[] group)
    {
        // determine the column width given the number of columns and the line width
        int columnWidth = LineLength / Columns;

        for (int i = 0; i < group.Length; i++)
        {
            if (i > 0 && i % Columns == 0)
            {   // Finished a complete line; write a new-line to start on the next one
                Console.WriteLine();
            }
            if (group[i].Length > columnWidth)
            {   // This word is too long; truncate it to the column width
                Console.WriteLine(group[i].Substring(0, columnWidth));
            }
            else
            {   // Write out the word with spaces padding it to fill the column width
                Console.Write(group[i].PadRight(columnWidth));
            }
        }
    }

If you call the above method with this sample code:

var groupOfWords = new String[] { "alphabet", "alegator", "ant", 
    "ardvark", "ark", "all", "amp", "ally", "alley" };
WriteGroup(groupOfWords);

Then you should get output that looks like this:

alphabet            alegator            ant                 ardvark
ark                 all                 amp                 ally
alley

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.