How to resolve the algorithm Align columns step by step in the Oforth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Align columns step by step in the Oforth programming language

Table of Contents

Problem Statement

Given a text file of many lines, where fields within a line are delineated by a single 'dollar' character, write a program that aligns each column of fields by ensuring that words in each column are separated by at least one space. Further, allow for each word in a column to be either left justified, right justified, or center justified within its column. Use the following text to test your programs:

Note that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Align columns step by step in the Oforth programming language

Source code in the oforth programming language

import: mapping
import: file

: <
   #[ ' ' <

String method: justify( n just -- s )
| l m |
   n self size - dup ->l 2 / ->m
   String new 
   just $RIGHT  if=: [ l <
   just $LEFT   if=: [ self <<  l <
   m <
;
 
: align( file just -- )
| lines maxsize |
    #[ wordsWith( '$' ) ] file File new map   ->lines
    0 #[ apply( #[ size max ] ) ] lines apply ->maxsize
    #[ apply( #[ justify( maxsize , just) . ] ) printcr ] lines apply
;

  

You may also check:How to resolve the algorithm Approximate equality step by step in the C programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Peloton programming language
You may also check:How to resolve the algorithm 24 game step by step in the OCaml programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Sidef programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Icon and Unicon programming language