Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone help me with the following problem. I have a load of string values returned from SQL Server in a dataset. I need loop round them and concatenate them together into strings which I can send as text messages to mobile phones. What I need to do is send them in the least number of text messages possible. As a text message has a maximum of 160 characters, I need to try and use up as much of that 160 characters as possible for each message. I can't split part of a value between text messages as this would not be meaningful to the recipient.

Can anyone help here - I haven't a clue where to start of this.
Many Thanks.
Posted

Try the following semi c# pseudo code
StringBuilder sb = new Stringbuilder();
foreach row in datatable.rows
{
    string val = GetDataFromRowColumn();
    if(sb.Length + val.Length> 160)
    {
        SendMessage(sb.Tostring());
        sb = new StringBuilder();
    }
    sb.Append(val);    
}   
if(sb.Length>0)
   SendMessage(sb.ToString());
 
Share this answer
 
Comments
GParkings 5-Sep-11 12:25pm    
Haha, essentially the same olution posted at exactly the same time :D ... have a 5
Mehdi Gholam 5-Sep-11 12:52pm    
HeHe, the same :D
MarkB123 5-Sep-11 12:31pm    
Hi, Thanks for the response.
Unfortunately it doesn't solve my problem. This will not send the data in the fewest number of text messages possible. I don't care about the order I send the data, only that they fit into as few messages as possible. What I need is that it attempts to get as close to the full 160 characters as possible to minimize the number of messages required.
Mehdi Gholam 5-Sep-11 12:50pm    
Ah, that's what I call fitting music on a CD problem. For an optimized solution you need to do simplex programming, this is difficult to do, a heuristic approach is to sort the strings descending by length then fill the messages that way.
MarkB123 6-Sep-11 4:56am    
Thanks Mehdi, I didn't know what the correct terminology was for this.
Assuming the strings need to be sent in the order they are received (I'll look into another solution in case this assumption is incorrect)

Try something along the lines of:

C#
IList<string> strings; //your string collection fromt he DB
IList<string> messages = new List<string>(); //collection of txt message strings
StringBuilder bldr = new StringBuilder();

for (int i = 0; i < strings.Count; i++)
{
    if (bldr.Length + strings[i].Length > 160) //160 should be in a constant
    {
        //adding the string would put us past 160, so the message is complete
        //'as-is'
        messages.Add(bldr.ToString()); // put the complete message in our list
        bldr.Clear(); //start a new message
    }

    bldr.Append(strings[i] + " "); //add the string to the current message

}

messages.Add(bldr.ToString()); //add the last message
 
Share this answer
 
v3
Comments
MarkB123 5-Sep-11 12:30pm    
Hi, Thanks for the response.
Unfortunately it doesn't solve my problem. This will not send the data in the fewest number of text messages possible. I don't care about the order I send the data, only that they fit into as few messages as possible. What I need is that it attempts to get as close to the full 160 characters as possible to minimize the number of messages required.
Since my assumption in my previous solution was incorrect and you are, in fact, looking for a 'best fit' algorithm that will attempt to fill messages with the minimum capacity wastage i did a bit of googling on your behalf and found this:

http://www.developerfusion.com/article/5540/bin-packing/5/

This example is for placing variable sized 'elements' into 'bins' but the concept should be the same if you think of the 160 character message as a 'bin' and the variable length strings as 'elements'

hope that helps or at least will suffice until one of the CP gods trumps it ;p
 
Share this answer
 
Comments
Mehdi Gholam 5-Sep-11 13:03pm    
HeHe, I just commented the same... interestingly in the articles comments someone refers to CD filling also.
MarkB123 5-Sep-11 13:18pm    
Thanks, I'll have a look at the link you supplied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900