views:

588

answers:

3

I have a dictionary object IDictionary<string, string> it only has the following items: Item1, Items2 and Item3. Each item only has a maximum length of 50 characters.

I then have a list of words List<string>. I need a loop that will go through the words and add them to the dictionary starting at Item1, but before adding it to the dictionary the length needs to be checked. If the new item and the current item's length added together is greater than 50 characters then the word needs to move down to the next line (in this case Item2)

What is the best way to do this?

+4  A: 

I'm not sure why this question got voted down so much, but perhaps the reason is you have a very clear algorithm already, so it should be trivial to get the C# code. As it stands, either you're either really inexperienced or really lazy. I'm going to assume the former.

Anyways, lets walk through the requirements.

1) "I then have a list of words List." you have this line in some form already.

List<string> words = GetListOfWords();

2) "go through the words and add them to the dictionary starting at Item1" -- I'd recommend a List instead of a dictionary, if you're going for a sequence of strings. Also, you'll need a temporary variable to store the contents of the current line, because you're really after adding a full line at a time.

var lines = new List<string>();   
string currentLine = "";

3) "I need a loop that will go through the words"

foreach(var word in words) {

4) " If the new item and the current item's length added together is greater than 50 characters" -- +1 for the space.

    if (currentLine.Length + word.Length + 1 > 50) {

5) "then the word needs to move down to the next line "

        lines.Add(currentLine);
        currentLine = word;    
    }

6) " go through the words and add them to the dictionary starting at Item1 " -- you didn't phrase this very clearly. What you meant was you want to join each word to the last line unless it would make the line exceed 50 characters.

    else {
        currentLine += " " + word;   
    }
}
lines.Add(currentLine); // the last unfinished line

and there you go.


If you absolutely need it as an IDictionary with 3 lines, just do

var dict = new Dictionary<string,string>();
for(int lineNum = 0; lineNum < 3; lineNum ++)
    dict["Address"+lineNum] = lineNume < lines.Length ? lines[lineNum] : "";
Jimmy
A: 

I currently have this and was wondering if there was perhaps a better solution:


public IDictionary AddWordsToDictionary(IList words)
{
    IDictionary addressParts = new Dictionary();
    addressParts.Add("Address1", string.Empty);
    addressParts.Add("Address2", string.Empty);
    addressParts.Add("Address3", string.Empty);

    int currentIndex = 1;
    foreach (string word in words)
    {
        if (!string.IsNullOrEmpty(word))
        {
            string key = string.Concat("Address", currentIndex);
            int space = 0;
            string spaceChar = string.Empty;
            if (!string.IsNullOrEmpty(addressParts[key]))
            {
                space = 1;
                spaceChar = " ";
            }
            if (word.Length + space + addressParts[key].Length > MaxAddressLineLength)
            {
                currentIndex++;
                key = string.Concat("Address", currentIndex);
                space = 0;
                spaceChar = string.Empty;
                if (currentIndex > addressParts.Count)
                {
                    break; // To big for all 3 elements so discard
                }
            }

            addressParts[key] = string.Concat(addressParts[key], spaceChar, word);
        }
    }

    return addressParts;
}
dnoxs
A: 

I would just do the following and do what you like with the resulting "lines"

 static List<string> BuildLines(List<string> words, int lineLen)
 {
  List<string> lines = new List<string>();
  string line = string.Empty;
  foreach (string word in words)
  {
   if (string.IsNullOrEmpty(word)) continue;

   if (line.Length + word.Length + 1 <= lineLen)
   {
    line += " " + word;
   }
   else
   {
    lines.Add(line);
    line = word;
   }
  }
  lines.Add(line);
  return lines;
 }
Lindholm