How to resolve the algorithm Repeat a string step by step in the PostScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Repeat a string step by step in the PostScript programming language

Table of Contents

Problem Statement

Take a string and repeat it some number of times.
Example: repeat("ha", 5)   =>   "hahahahaha" If there is a simpler/more efficient way to repeat a single “character” (i.e. creating a string filled with a certain character), you might want to show that as well (i.e. repeat-char("*", 5) => "*****").

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Repeat a string step by step in the PostScript programming language

Source code in the postscript programming language

% the comments show the stack content after the line was executed
% where rcount is the repeat count, "o" is for orignal,
% "f" is for final, and iter is the for loop variable
%
% usage: rcount ostring times -> fstring

/times {
  dup length dup    % rcount ostring olength olength
  4 3 roll          % ostring olength olength rcount
  mul dup string    % ostring olength flength fstring
  4 1 roll          % fstring ostring olength flength
  1 sub 0 3 1 roll  % fstring ostring 0 olength flength_minus_one 
  {                 % fstring ostring iter
    1 index 3 index % fstring ostring iter ostring fstring
    3 1 roll        % fstring ostring fstring iter ostring
    putinterval     % fstring ostring
  } for
  pop               % fstring
} def

  

You may also check:How to resolve the algorithm Averages/Median step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Window management step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Amb step by step in the BASIC programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the REXX programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Maxima programming language