1 Replies - 888 Views - Last Post: 07 April 2012 - 06:10 AM Rate Topic: -----

#1 MitchellT   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 30-August 11

Lines of strings which contain the same amount of strings [Proble

Posted 06 April 2012 - 11:45 AM

Hi

I have a text file which contains sequences of letters and/or number's e.g.

"Offa was the King of Mercia from 757 until his death in July 796. The son of Thingfrith and a descendant of Eowa, Offa came to the throne after a period of civil war. In the early years of Offa's reign it is likely that he consolidated his control of midland peoples such as the Hwicce and the Magonsæte. Taking advantage of instability in the kingdom of Kent to establish himself as overlord, Offa was also in control of Sussex by 771, though his authority did not remain unchallenged in either territory"

My problem is that I have to get the same amount of words on each line but at the same time I can't go over the character line limit e.g.

Character Line Limit = 10

1 22 333 4444 55555 666666 7777777

Solution:

1 22 333 4444 (10 characters on this line)
55555 (5 characters on this line)
666666 (6 characters on this line)
7777777 (7 characters on this line)

The above solution is incorrect because each line contains a different number of words (although it is staying within the line limit), my problem requires me to have the same number of words per line without exceeding the line limit, so going on what I have said the correct solution to the above would be:

1
22
333
4444
55555
666666
7777777

This is because 7777777 and 666666 together with a space separating them would be in total 14 characters (also the same for 55555 and 666666 and so on...), therefore the maximum words per line is 1, but how am I supposed to check this in C#, this is the main problem, I am currently using a string builder with no success.

Conclusion of what I have thought of so far:

1. If I specify the line length to be smaller than the largest word in the text file it will crash the program, so I will need to check for this.

2. Initially I thought of finding all the biggest words until I reached the line length and having this as the "words per line" for every line, but this idea will not work, because the largest words may not be together and therefore there could (potentially) be more words on a line.

3. If I am given 5 words of length 5 characters and 2 words fit on a line (line limit = 10), this means I will have 3 lines, 2 lines with 2 words, and 1 line with one word, this then goes against what I have said above and therefore the solution to this is to put one word on each line, would you agree?

------

What I have implemented:

1. A method which reads in a text file
2. Splits the text based on white spaces
3. Puts each word in an array of strings

------

The problem I am having is a logical problem, I have already implemented some code, but without any success for what I am trying to achieve above, if anyone could help with this problem then I will be able to implement some of the solutions in C# and see where I go from there.

Any help is appreciated

Thanks

This post has been edited by MitchellT: 06 April 2012 - 11:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Lines of strings which contain the same amount of strings [Proble

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Lines of strings which contain the same amount of strings [Proble

Posted 07 April 2012 - 06:10 AM

let's see your code,post your code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1