How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Action! 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 Action! 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 Action! programming language
Source code in the action! programming language
DEFINE SPACE="32"
DEFINE TAB="127"
DEFINE NEWLINE="155"
BYTE FUNC IsWhitespace(CHAR c)
IF c=SPACE OR c=TAB OR c=NEWLINE THEN
RETURN (1)
FI
RETURN (0)
PROC Strip(CHAR ARRAY src,dst BYTE head,tail)
BYTE i,first,last
first=1
last=src(0)
IF head THEN
WHILE first<=last
DO
IF IsWhitespace(src(first))=0 THEN
EXIT
FI
first==+1
OD
FI
IF tail THEN
WHILE last>=first
DO
IF IsWhitespace(src(last))=0 THEN
EXIT
FI
last==-1
OD
FI
IF first>last THEN
dst(0)=0
ELSE
SCopyS(dst,src,first,last)
FI
RETURN
PROC Main()
CHAR ARRAY src=" Action! ",dst(20)
src(2)=NEWLINE src(13)=TAB
PrintF("Original string:%E""%S""%E%E",src)
Strip(src,dst,1,0)
PrintF("Trim leading whitespaces:%E""%S""%E%E",dst)
Strip(src,dst,0,1)
PrintF("Trim trailing whitespaces:%E""%S""%E%E",dst)
Strip(src,dst,1,1)
PrintF("Trim leading and trailing whitespaces:""%S""%E%E",dst)
RETURN
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Java programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the M4 programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Read entire file step by step in the Stata programming language