Re: ChatGPT & Euphoria

new topic     » goto parent     » topic index » view thread      » older message » newer message

write a function to break a string up into words, each word in quotes, separated by commas, and add comments 

-- This function takes a string, splits it into words, 
-- surrounds each word with double quotes, and joins them with commas. 
function quote_and_comma_words(sequence input) 
    sequence result = ""   -- Final result string 
    sequence word = ""     -- Temporary storage for building each word 
    integer first = 1      -- Flag to handle whether to prepend a comma 
 
    -- Loop through each character in the input 
    for i = 1 to length(input) do 
        integer ch = input[i] 
 
        if ch = ' ' then 
            -- We've hit a space; if we have a word built, add it to result 
            if length(word) > 0 then 
                if not first then 
                    result &= ","      -- Add comma before next word (not the first) 
                else 
                    first = 0          -- Mark that the first word is done 
                end if 
                result &= "\"" & word & "\""  -- Add quoted word 
                word = ""             -- Reset word builder 
            end if 
        else 
            -- Add character to current word 
            word &= ch 
        end if 
    end for 
 
    -- Handle the last word (if there is one) 
    if length(word) > 0 then 
        if not first then 
            result &= "," 
        end if 
        result &= "\"" & word & "\"" 
    end if 
 
    return result 
end function 
 
 
puts(1,quote_and_comma_words("Now is the time for all good men")) 
puts(1,'\n') 

irv@irv-desktop:~$ eui quotem.ex  
"Now","is","the","time","for","all","good","men" 

This isn't bad for writing small functions. Also works for reformatting code to look neater, and adds reasonable comments.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu