tags:

views:

41

answers:

1

When replying to an SMS, I have a limit of 160 characters. I currently have code set up to take a reply (which can be >160) and split it into a list of multiple texts each <160. It's also set up so that it keeps words whole. I included it:

repl='message to be sent. may be >160'
texts=[]
words=repl.split()
curtext=''
for word in words:
    #for the first word, drop the space
    if len(curtext)==0:
        curtext+=word

    #check if there's enough space left in the current message
    elif len(curtext)<=155-(len(word)+1):
        curtext+=' '+word

    #not enough space. make a new message
    else:
        texts.append(curtext)
        curtext=word
if curtext!='':
    texts.append(curtext)
return texts

However, I now want to modify it so that it appends "reply m for more" to end of every second message. Any ideas on how to do this?

(I'm writing code in Python)

+1  A: 
reply = "text to be sent ...."
texts = []

count = 0
current_text = []
for word in reply.split():
   if count + len(word) < (160 if len(texts) % 2 == 0 else (160-17)):
      current_text.append(word)
      count += (len(word) + 1)
   else:
      count = 0
      if len(texts) % 2 != 0):
         #odd-numbered text gets additional message...
         texts.append(" ".join(current_text) + "\nreply m for more")
      else:
         texts.append(" ".join(current_text))
   current_text = []
gilesc