Comma quibbling: Difference between revisions

No edit summary
Line 2,006:
[ABC,DEF,G,H] => {ABC, DEF, G and H}
true.</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>
EnableExplicit
 
Procedure.s CommaQuibble(Input$)
Protected i, count
Protected result$, word$
Input$ = RemoveString(Input$, "[")
Input$ = RemoveString(Input$, "]")
Input$ = RemoveString(Input$, #DQUOTE$)
count = CountString(Input$, ",") + 1
result$ = "{"
For i = 1 To count
word$ = StringField(Input$, i, ",")
If i = 1
result$ + word$
ElseIf Count = i
result$ + " and " + word$
Else
result$ + ", " + word$
EndIf
Next
ProcedureReturn result$ + "}"
EndProcedure
 
If OpenConsole()
; As 3 of the strings contain embedded quotes these need to be escaped with '\' and the whole string preceded by '~'
PrintN(CommaQuibble("[]"))
PrintN(CommaQuibble(~"[\"ABC\"]"))
PrintN(CommaQuibble(~"[\"ABC\",\"DEF\"]"))
PrintN(CommaQuibble(~"[\"ABC\",\"DEF\",\"G\",\"H\"]"))
PrintN("")
PrintN("Press any key to close the console")
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
EndIf
</lang>
 
{{out}}
<pre>
{}
{ABC}
{ABC and DEF}
{ABC, DEF, G and H}
</pre>
 
=={{header|Python}}==
9,492

edits