How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Seed7 programming language

Table of Contents

Problem Statement

Strip control codes and extended characters from a string.

The solution should demonstrate how to achieve each of the following results:

In ASCII, the control codes have decimal codes 0 through to 31 and 127. On an ASCII based system, if the control codes are stripped, the resultant string would have all of its characters within the range of 32 to 126 decimal on the ASCII table. On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";
  include "utf8.s7i";

const func string: stripControl (in string: stri) is func
  result
    var string: stripped is "";
  local
    var integer: old_pos is 1;
    var integer: index is 0;
    var char: ch is ' ';
  begin
    for ch key index range stri do
      if ch < ' ' or ch = '\127;' then
        stripped &:= stri[old_pos .. pred(index)];
        old_pos := succ(index);
      end if;
    end for;
    stripped &:= stri[old_pos ..];
  end func;

const func string: stripControlAndExtended (in string: stri) is func
  result
    var string: stripped is "";
  local
    var integer: old_pos is 1;
    var integer: index is 0;
    var char: ch is ' ';
  begin
    for ch key index range stri do
      if ch < ' ' or ch >= '\127;' then
        stripped &:= stri[old_pos .. pred(index)];
        old_pos := succ(index);
      end if;
    end for;
    stripped &:= stri[old_pos ..];
  end func;

const string: src is "déjà vu\              # Unicode
    \\n\0;\31; \33;\126;\127;\128;\255;\n\  # Various boundary cases
    \as⃝df̅";                                 # Unicode combining characters

const proc: main is func
  begin
    OUT := STD_UTF8_OUT;
    writeln("source text:");
    writeln(src);
    writeln("Stripped of control codes:");
    writeln(stripControl(src));
    writeln("Stripped of control codes and extended characters:");
    writeln(stripControlAndExtended(src));
  end func;

  

You may also check:How to resolve the algorithm Polynomial long division step by step in the SPAD programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the MINIL programming language
You may also check:How to resolve the algorithm Parameterized SQL statement step by step in the Objeck programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the D programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Oforth programming language