How to resolve the algorithm Determine sentence type step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine sentence type step by step in the CLU programming language

Table of Contents

Problem Statement

Use these sentences: "hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it."

Don't leave any errors!

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine sentence type step by step in the CLU programming language

Source code in the clu programming language

% This iterator takes a string and yields one of 'E', 'Q',
% 'S' or 'N' for every sentence found.
% Because sentences are separated by punctuation, only the
% last one can be 'N'.

sentence_types = iter (s: string) yields (char)
    own punct: string := "!?."  % relevant character classes
    own space: string := " \t\n"
    own types: string := "EQS"  % sentence type characters
    
    prev_punct: bool := false   % whether the previous character was punctuation
    last_punct: int := 0        % index of last punctuation character encountered
    sentence: bool := true      % whether there are words since the last punctuation
    
    for c: char in string$chars(s) do
        pu: int := string$indexc(c, punct)
        sp: int := string$indexc(c, space)
        if pu ~= 0 then
            prev_punct := true
            last_punct := pu 
        elseif sp ~= 0 then
            if prev_punct then
                % a space after punctuation means a sentence has ended here
                yield(types[last_punct])
                sentence := false
            end
            prev_punct := false
            sentence := false
        else
            sentence := true
        end
    end
    
    % handle the last sentence
    if prev_punct then yield(types[last_punct])
    elseif sentence then yield('N')
    end
end sentence_types

% Test
start_up = proc ()
    po: stream := stream$primary_output()
    test: string := 
        "hi there, how are you today? I'd like to " ||
        "present to you the washing machine 9001. You " ||
        "have been nominated to win one of these! Just " ||
        "make sure you don't break it" 

    % print the type of each sentence
    for c: char in sentence_types(test) do  
        stream$putc(po, c)
    end
end start_up

  

You may also check:How to resolve the algorithm 100 doors step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Create an object at a given address step by step in the Nim programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the Go programming language