How to resolve the algorithm Tokenize a string with escaping step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Tokenize a string with escaping step by step in the CLU programming language

Table of Contents

Problem Statement

Write a function or program that can split a string at each non-escaped occurrence of a separator character. It should accept three input parameters:

It should output a list of strings. Rules for splitting:

Rules for escaping:

Demonstrate that your function satisfies the following test-case: (Print the output list in any format you like, as long as it is it easy to see what the fields are.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Tokenize a string with escaping step by step in the CLU programming language

Source code in the clu programming language

tokenize = iter (sep, esc: char, s: string) yields (string)
    escape: bool := false
    part: array[char] := array[char]$[]
    for c: char in string$chars(s) do   
        if escape then 
            escape := false
            array[char]$addh(part,c)
        elseif c=esc then
            escape := true
        elseif c=sep then
            yield(string$ac2s(part))
            part := array[char]$[]
        else
            array[char]$addh(part,c)
        end
    end
    yield(string$ac2s(part))
end tokenize

start_up = proc ()
    po: stream := stream$primary_output()
    testcase: string := "one^|uno||three^^^^|four^^^|^quatro|"
    
    for part: string in tokenize('|', '^', testcase) do
        stream$putl(po, "\"" || part || "\"")
    end
end start_up

  

You may also check:How to resolve the algorithm Count in octal step by step in the Bracmat programming language
You may also check:How to resolve the algorithm String matching step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the Fermat programming language
You may also check:How to resolve the algorithm Comments step by step in the NESL programming language
You may also check:How to resolve the algorithm Binary digits step by step in the VBA programming language