Comma quibbling is a task originally set by Eric Lippert in his blog.



Task

Write a function to generate a string output which is the concatenation of input words from a list/sequence where:

 An input of no words produces the output string of just the two brace characters "{}".
 An input of just one word, e.g. ["ABC"], produces the output string of the word inside the two braces, e.g. "{ABC}".
 An input of two words, e.g. ["ABC", "DEF"], produces the output string of the two words inside the two braces with the words separated by the string " and ", e.g. "{ABC and DEF}".
 An input of three or more words, e.g. ["ABC", "DEF", "G", "H"], produces the output string of all but the last word separated by ", " with the last word separated by " and " and all within braces; e.g. "{ABC, DEF, G and H}".


Test your function with the following series of inputs showing your output here on this page:

 [] # (No input words).
 ["ABC"]
 ["ABC", "DEF"]
 ["ABC", "DEF", "G", "H"]


Note: Assume words are non-empty strings of uppercase characters for this task.




DEFINE PTR="CARD"
 
PROC Append(CHAR ARRAY text,suffix)
  BYTE POINTER srcPtr,dstPtr
  BYTE len
 
  len=suffix(0)
  IF text(0)+len>255 THEN
    len=255-text(0)
  FI
  IF len THEN
    srcPtr=suffix+1
    dstPtr=text+text(0)+1
    MoveBlock(dstPtr,srcPtr,len)
    text(0)==+suffix(0)
  FI
RETURN
 
PROC Quibble(PTR ARRAY items INT count CHAR ARRAY result)
  INT i
 
  result(0)=0
  Append(result,"(")
  FOR i=0 TO count-1
  DO
    Append(result,items(i))
    IF i=count-2 THEN
      Append(result," and ")
    ELSEIF i<count-2 THEN
      Append(result,", ")
    FI
  OD
  Append(result,")")
RETURN
 
PROC Test(PTR ARRAY items BYTE count)
  CHAR ARRAY result(256)
 
  Quibble(items,count,result)
  PrintE(result)
RETURN
 
PROC Main()
  PTR ARRAY items(5)
 
  Test(items,0)
 
  items(0)="ABC"
  Test(items,1)
 
  items(1)="DEF"
  Test(items,2)
 
  items(2)="G"
  Test(items,3)
 
  items(3)="H"
  Test(items,4)
RETURN