About

The wasSwapWords function exchanges two words in a sequence of token-delimited words and returns the resulting sequence.

For example:

Say wasSwapWords('Search/For/Me', 'Search', 'Dream', '/')

will output: Dream/For/Me

Code

/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
wasSwapWords: procedure /* Swap two words in a token delimited string.   */
    Parse ARG String,AWord,BWord,Token
    If String = '' Then Return ''
    Parse VAR String Head Interpret(Token) Tail
    Select
        When Head = AWord Then Head = BWord
        When Head = BWord Then Head = AWord
        Otherwise Nop
    End
    Look = wasSwapWords(Tail,AWord,BWord,Token)
    If Look = '' Then Return Head
Return Head || Token || Look