How to resolve the algorithm Substring step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Substring step by step in the PARI/GP programming language

Table of Contents

Problem Statement

Display a substring:

If the program uses UTF-8 or UTF-16,   it must work on any valid Unicode code point, whether in the   Basic Multilingual Plane   or above it. The program must reference logical characters (code points),   not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Substring step by step in the PARI/GP programming language

Source code in the pari/gp programming language

\\ Returns the substring of string str specified by the start position s and length n.
\\ If n=0 then to the end of str.
\\ ssubstr() 3/5/16 aev
ssubstr(str,s=1,n=0)={
my(vt=Vecsmall(str),ve,vr,vtn=#str,n1);
if(vtn==0,return(""));
if(s<1||s>vtn,return(str));
n1=vtn-s+1; if(n==0,n=n1); if(n>n1,n=n1);
ve=vector(n,z,z-1+s); vr=vecextract(vt,ve); return(Strchr(vr));
}

{\\ TEST
my(s="ABCDEFG",ns=#s);
print(" *** Testing ssubstr():");
print("1.",ssubstr(s,2,3));
print("2.",ssubstr(s));
print("3.",ssubstr(s,,ns-1));
print("4.",ssubstr(s,2));
print("5.",ssubstr(s,,4));
print("6.",ssubstr(s,0,4));
print("7.",ssubstr(s,3,7));
print("8.|",ssubstr("",1,4),"|");
}

  

You may also check:How to resolve the algorithm Factors of an integer step by step in the Red programming language
You may also check:How to resolve the algorithm Documentation step by step in the PHP programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the OCaml programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Phix programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Icon and Unicon programming language