How to resolve the algorithm Determine if a string is collapsible step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determine if a string is collapsible step by step in the Cowgol programming language

Table of Contents

Problem Statement

Determine if a character string is   collapsible. And if so,   collapse the string   (by removing   immediately repeated   characters).

If a character string has   immediately repeated   character(s),   the repeated characters are to be deleted (removed),   but not the primary (1st) character(s).

An   immediately repeated   character is any character that is   immediately   followed by an identical character (or characters).   Another word choice could've been   duplicated character,   but that might have ruled out   (to some readers)   triplicated characters   ···   or more.

{This Rosetta Code task was inspired by a newly introduced   (as of around November 2019)   PL/I   BIF:   collapse.}

In the following character string:

Only the 2nd   t,   e, and   l   are repeated characters,   indicated by underscores (above),   even though they (those characters) appear elsewhere in the character string.

So, after collapsing the string, the result would be:

Another example: In the following character string:

The "collapsed" string would be:

Write a subroutine/function/procedure/routine···   to locate   repeated   characters and   collapse   (delete)   them from the character string.   The character string can be processed from either direction.

Show all output here, on this page:

Use (at least) the following five strings,   all strings are length seventy-two (characters, including blanks),   except the 1st string:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string is collapsible step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";
include "strings.coh";

# Collapse the string at in, and store the result in the given buffer
sub collapse(in: [uint8], out: [uint8]) is
    var ch := [in];
    in := @next in; 
    loop
        if ch == 0 then
            [out] := 0;
            return;
        elseif [in] != ch then
            [out] := ch;
            out := @next out;
            ch := [in];
        end if;
        in := @next in;
    end loop;
end sub;

# Given a string, collapse it and print all required output
sub show(str: [uint8]) is
    sub bracket_length(str: [uint8]) is
        print_i32(StrLen(str) as uint32);
        print(" <<<");
        print(str);
        print(">>>");
        print_nl();
    end sub;
    
    var buf: uint8[256];
    collapse(str, &buf[0]);
    bracket_length(str);
    bracket_length(&buf[0]);
    print_nl();
end sub;

# Strings from the task
var strings: [uint8][] := {
"",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111117777888",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
"                                                    --- Harry S Truman  "
};

# Collapse and print each string
var i: @indexof strings := 0;
while i < @sizeof strings loop
    show(strings[i]);
    i := i + 1;
end loop;

  

You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Animation step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Picat programming language
You may also check:How to resolve the algorithm Fairshare between two and more step by step in the Phix programming language
You may also check:How to resolve the algorithm Hash join step by step in the Swift programming language