How to resolve the algorithm Strip comments from a string step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Strip comments from a string step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

The task is to remove text that follow any of a set of comment markers, (in these examples either a hash or a semicolon) from a string or input line.

Whitespace debacle:   There is some confusion about whether to remove any whitespace from the input line. As of 2 September 2011, at least 8 languages (C, C++, Java, Perl, Python, Ruby, sed, UNIX Shell) were incorrect, out of 36 total languages, because they did not trim whitespace by 29 March 2011 rules. Some other languages might be incorrect for the same reason. Please discuss this issue at Talk:Strip comments from a string.

The following examples will be truncated to either "apples, pears " or "apples, pears". (This example has flipped between "apples, pears " and "apples, pears" in the past.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strip comments from a string step by step in the ALGOL 68 programming language

Source code in the algol programming language

#!/usr/local/bin/a68g --script #

PROC trim comment = (STRING line, CHAR marker)STRING:(
  INT index := UPB line+1;
  char in string(marker, index, line);
  FOR i FROM index-1 BY -1 TO LWB line
  WHILE line[i]=" " DO index := i OD;
  line[:index-1]
);

CHAR q = """";

print((
  q, trim comment("apples, pears # and bananas", "#"), q, new line,
  q, trim comment("apples, pears ; and bananas", ";"), q, new line,
  q, trim comment("apples, pears and bananas  ", ";"), q, new line,
  q, trim comment("    ", ";"), q, new line, # blank string #
  q, trim comment("", ";"), q, new line  # empty string #
))

CO Alternatively Algol68g has available "grep"
;STRING re marker := " *#", line := "apples, pears # and bananas";
  INT index := UPB line;
  grep in string(re marker, line, index, NIL);
  print((q, line[:index-1], q, new line))
END CO

  

You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Perl programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Currying step by step in the Delphi programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the C3 programming language
You may also check:How to resolve the algorithm Stream merge step by step in the Fortran programming language