CamelCase in Icon

Example for versions iconc 9.4

First of all the program reads the string to process and adds a space to its end (|| is concatenation operator). After this text variable is scanned. ? is an operator which binds a string to the expression, so that all string matching functions in the expression are performed on this string.

Commands ReFind and ReMatch from regular expressions library regexp both find all sequences of symbols which match the given regex, but ReFind returns the index of sequence start, and ReMatch — the index of the first character after sequence end. In one iteration ReFind finds the start of the next sequence of non-letter characters. Command tab moves current position pointer to this position and returns part of the string from previous position of the pointer to the new position — a word. After this the word is converted to proper case and concatenated to the resulting string. *word is a function which returns length of the string. map replaces all characters of its first argument which are present in its second argument with corresponding characters from its third argument (in this case — switch case of characters; &lcase and &ucase are built-in constants which contain lowercase and uppercase alphabet). Finally, one more call of tab moves the pointer past the sequence of non-letter characters to the start of next word.

link regexp

procedure main () 
    text := read() || " ";
    cc := "";
    text ? {
        while j := ReFind("[^a-zA-Z]+") do {
            word := tab(j);
            cc ||:= map(word[1],&lcase,&ucase) || map(word[2:*word+1],&ucase,&lcase);
            tab(ReMatch("[^a-zA-Z]+"));
        }
    }
    write (cc);
end