How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Oberon-2 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Oberon-2 programming language
Table of Contents
Problem Statement
Demonstrate how to strip leading and trailing whitespace from a string. The solution should demonstrate how to achieve the following three results:
For the purposes of this task whitespace includes non printable characters such as the space character, the tab character, and other such characters that have no corresponding graphical representation.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE Trim;
IMPORT Out,Strings,SYSTEM;
CONST
(* whitespaces *)
HT = 09X; VT = 0BX; FF = 0CX; GS = 1DX; US = 1FX;
LF = 0AX; CR = 0DX; FS = 1CX; RS = 1EX; SPC = 20X;
PROCEDURE LTrim(VAR s: ARRAY OF CHAR);
VAR
j : INTEGER;
BEGIN
j := 0;
WHILE (s[j] = HT) OR (s[j] = LF) OR (s[j] = VT) OR (s[j] = CR) OR
(s[j] = FF) OR (s[j] = FS) OR (s[j] = FS) OR (s[j] = GS) OR
(s[j] = RS) OR (s[j] = US) OR (s[j] = SPC) DO INC(j) END;
SYSTEM.MOVE(SYSTEM.ADR(s[j]),SYSTEM.ADR(s[0]),LEN(s) - j);
END LTrim;
PROCEDURE RTrim(VAR s: ARRAY OF CHAR);
VAR
j : INTEGER;
BEGIN
j := LEN(s) - 1;
WHILE (j >= 0) & (s[j] = 0X) DO DEC(j) END;
WHILE (j >= 0) & ((s[j] = HT) OR (s[j] = LF) OR (s[j] = VT) OR (s[j] = CR) OR
(s[j] = FF) OR (s[j] = FS) OR (s[j] = FS) OR (s[j] = GS) OR
(s[j] = RS) OR (s[j] = US) OR (s[j] = SPC)) DO
s[j] := 0X;
DEC(j)
END
END RTrim;
PROCEDURE Trim(VAR s: ARRAY OF CHAR);
BEGIN
LTrim(s);
RTrim(s)
END Trim;
VAR
s: ARRAY 100 OF CHAR;
BEGIN
s := " A AAA";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := "AAA A ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " A AA A ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);Trim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);Trim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
END Trim.
You may also check:How to resolve the algorithm Cramer's rule step by step in the Rust programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Wren programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the BCPL programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the Ruby programming language