String matching: Difference between revisions

m
→‎{{header|REXX}}: corrected a spelling error, added comments and more whitespace within literals, use "true" IFs, removed FOREVER from DO statement.
m (→‎{{header|REXX}}: typo (contains -> contain)
m (→‎{{header|REXX}}: corrected a spelling error, added comments and more whitespace within literals, use "true" IFs, removed FOREVER from DO statement.)
Line 1,994:
Extra coding was added to take care of using plurals in the output messages.
<lang rexx>/*REXX program demonstrates some basic character string testing. */
parse arg a b /*obtain A and B from the C.L. */
parse arg a b
say 'string A = ' a /*display string A to terminal.*/
say 'string B = ' b /* " " B " " */
say
if left(A,length(b))==b then say 'string A starts with string B'
 
if left(A,length(b))==b then else say '"string A starts doesn't start with string B'"
else say "string A doesn't start with string B"
say
 
/*another method, however a wee bit obtuse. */
/*¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
Line 2,008 ⟶ 2,006:
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 contains stringdoesn't Bcontain (startingstring in position' p")B"
else say "'string A doesn't containcontains string B (starting in position' p")"
say
if right(A,length(b))==b then say 'string A ends with string B'
 
if right(A,length(b))==b then else say '"string A ends doesn't end with string B'"
else say "string A doesn't end with string B"
say
Ps=''; p=0; do foreveruntil until p==0
p = pos(b, a, p+1)
if p\==0 then Ps = Ps',' p
end /*forever until p==0···*/
Ps = space(strip(Ps, 'L', ","))
times = words(Ps)
if times\==0 then say '"string A contains doesn't contain string B',"
else say 'string A contains string B times 'time'left('s',times>1),
"(at position"times 'time'left('s',times>1) Ps')',
else say "string A doesn't contains string B "(at position"left('s', times>1) Ps')'
 
/*stick a fork in it, we're done.*/</lang>
'''output''' when the following is specified (five Marx brothers): &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo p </tt>
<pre>
<pre style="overflow:scroll">
string A = Chico_Harpo_Groucho_Zeppo_Gummo
string B = p
 
string A doesn't start with string B
 
string A contains string B (starting in position 10)
 
string A doesn't end with string B
 
string A contains string B 3 times (at positions 10, 23, 24)
</pre>
'''output''' when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Z </tt>
<pre>
<pre style="overflow:scroll">
string A = Chico_Harpo_Groucho_Zeppo_Gummo
string B = Z
 
string A doesn't start with string B
 
string A contains string B (starting in position 21)
 
string A doesn't end with string B
 
string A contains string B 1 time (at position 21)
</pre>
'''output''' when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo Chi </tt>
<pre>
<pre style="overflow:scroll">
string A = Chico_Harpo_Groucho_Zeppo_Gummo
string B = Chi
 
string A starts with string B
 
string A contains string B (starting in position 1)
string A doesn't end with string B
 
string A doesn't end withcontains string B 1 time (at position 1)
</pre>
 
'''output''' when the following is specified: &nbsp; <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt>
string A contains string B 1 time (at position 1)
</pre>
'''output'''string when theA following is= specified: <tt> Chico_Harpo_Groucho_Zeppo_Gummo mmo </tt>
string B = mmo
<pre style="overflow:scroll">
string A= Chico_Harpo_Groucho_Zeppo_Gummo
string B= mmo
 
string A doesn't start with string B
 
string A contains string B (starting in position 29)
 
string A ends with string B
 
string A contains string B 1 time (at position 29)
</pre>