RosettaCodeData/Task/Comma-quibbling/PureBasic/comma-quibbling.purebasic

35 lines
947 B
Plaintext

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