How to resolve the algorithm Strip comments from a string step by step in the Seed7 programming language
How to resolve the algorithm Strip comments from a string step by step in the Seed7 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 Seed7 programming language
Source code in the seed7 programming language
$ include "seed7_05.s7i";
const func string: stripComment (in string: line) is func
result
var string: lineWithoutComment is "";
local
var integer: lineEnd is 0;
var integer: pos is 0;
begin
lineEnd := length(line);
for pos range 1 to length(line) do
if line[pos] in {'#', ';'} then
lineEnd := pred(pos);
pos := length(line);
end if;
end for;
lineWithoutComment := line[.. lineEnd];
end func;
const proc: main is func
local
var string: stri is "apples, pears # and bananas\n\
\apples, pears ; and bananas";
var string: line is ""
begin
writeln(stri);
writeln("====>");
for line range split(stri, '\n') do
writeln(stripComment(line));
end for;
end func;
You may also check:How to resolve the algorithm Lucky and even lucky numbers step by step in the Ring programming language
You may also check:How to resolve the algorithm Truncate a file step by step in the D programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the Wren programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the Ruby programming language