String matching: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, added whitespace to the output, boxed a couple of statements (as a comment), simplified some IFs, made capitalization of A and B variables, used the improved {{out}} template.
m (added indentation for the numbered bullet points.)
m (→‎{{header|REXX}}: added/changed comments and whitespace, added whitespace to the output, boxed a couple of statements (as a comment), simplified some IFs, made capitalization of A and B variables, used the improved {{out}} template.)
Line 2,438:
=={{header|REXX}}==
Extra coding was added to take care of using plurals in the output messages.
<lang rexx>/*REXX program demonstrates some basic character string testing (for matching). */
parse arg a b A B; LB=length(B) /*obtain A and B from the Ccommand line.L. */
say 'string A = ' aA /*display string A to the terminal.*/
say 'string B = ' bB /* " " B " " " */
say
if left(A,length(b) LB)==bB then say 'string A starts with string B'
else say "string A doesn't start with string B"
say /* [] another above is a bigmethod comment.using COMPARE BIF*/
/*╔══════════════════════════════════════════════════════════════════════════╗
if compare(aA,bB)==length(b)LB then say 'string A starts with string B'
else say "string A doesn't start with string B"
╚══════════════════════════════════════════════════════════════════════════╝*/
p=pos(bB,a A)
if p==0 then say "string A doesn't contain string B"
else say 'string A contains string B (starting in position' p")"
say
if right(A,length(b) LB)==b then say 'string A ends with string B'
/*another method, however a wee bit obtuse. */
else say '"string A containsdoesn't end with string B ',"
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
if compare(a,b)==length(b) then say 'string A starts with string B'
else say "string A doesn't start with string B"
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬*/
/* [↑] above is a big comment. */
p=pos(b,a)
if p==0 then say "string A doesn't contain string B"
else say 'string A contains string B (starting in position' p")"
say
$=; p=0; do until p==0; p=pos(bB, aA, p+1)
if right(A,length(b))==b then say 'string A ends with string B'
else sayif "stringp\==0 Athen doesn$=$','t end with string B"p
/*anotherend method, however a wee bit obtuse./*until ···*/
say
Ps$=space(strip($,'L';,",")) p=0; do /*elide untilextra blanks p==0and leading comma.*/
times#=words(Ps$)
p=pos(b, a, p+1)
if times#==0 then say "string A doesn't contain string B"
if p\==0 then Ps = Ps',' p
else say 'string A contains string B ' # " time"left('s', end /*until ···*/#>1),
"(at position"left('s', times #>1) Ps'$")'"
Ps=space(strip(Ps, 'L', ","))
/*stick a fork in it, we're all done. */</lang>
times=words(Ps)
{{out}}|output|text=&nbsp; when the following is specified (the five Marx brothers): &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt> }}
if times==0 then say "string A doesn't contain string B"
else say 'string A contains string B ',
times 'time'left('s',times>1),
"(at position"left('s', times>1) Ps')'
 
/*stick a fork in it, we're done.*/</lang>
{{out}} when the following is specified (five Marx brothers): &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt>
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,482 ⟶ 2,477:
string A doesn't end with string B
 
string A contains string B 3 times (at positions 10, 23, 24)
</pre>
{{out}}|output|text=&nbsp; when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Z </tt> }}
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,495 ⟶ 2,490:
string A doesn't end with string B
 
string A contains string B 1 time (at position 21)
</pre>
{{out}}|output|text=&nbsp; when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Chi </tt> }}
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,508 ⟶ 2,503:
string A doesn't end with string B
 
string A contains string B 1 time (at position 1)
</pre>
{{out}}|output|text=&nbsp; when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt> }}
<pre>
string A = Chico_Harpo_Groucho_Zeppo_Gummo
Line 2,521 ⟶ 2,516:
string A ends with string B
 
string A contains string B 1 time (at position 29)
</pre>