How to resolve the algorithm Search a list step by step in the PL/I programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Search a list step by step in the PL/I programming language
Table of Contents
Problem Statement
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack). Raise an exception if the needle is missing. If there is more than one occurrence then return the smallest index to the needle. Return the largest index to a needle that has multiple occurrences in the haystack.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list step by step in the PL/I programming language
Source code in the pl/i programming language
search: procedure () returns (fixed binary);
declare haystack (0:9) character (200) varying static initial
('apple', 'banana', 'celery', 'dumpling', 'egg', 'flour',
'grape', 'pomegranate', 'raisin', 'sugar' );
declare needle character (200) varying;
declare i fixed binary;
declare missing_needle condition;
on condition(missing_needle) begin;
put skip list ('your string ''' || needle ||
''' does not exist in the haystack.');
end;
put ('Please type a string');
get edit (needle) (L);
do i = lbound(haystack,1) to hbound(haystack,1);
if needle = haystack(i) then return (i);
end;
signal condition(missing_needle);
return (lbound(haystack,1)-1);
end search;
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the Swift programming language
You may also check:How to resolve the algorithm System time step by step in the jq programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the Ruby programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the Rascal programming language
You may also check:How to resolve the algorithm Literals/String step by step in the F# programming language