String matching: Difference between revisions

Added solution for Action!
No edit summary
(Added solution for Action!)
Line 340:
.include "../includeARM64.inc"
</lang>
=={{header|Action!}}==
<lang Action!>BYTE FUNC FindS(CHAR ARRAY text,sub BYTE start)
BYTE i,j,found
 
i=start
WHILE i<=text(0)-sub(0)+1
DO
found=0
FOR j=1 TO sub(0)
DO
IF text(i+j-1)#sub(j) THEN
found=0 EXIT
ELSE
found=1
FI
OD
IF found THEN
RETURN (i)
FI
i==+1
OD
RETURN (0)
 
BYTE FUNC StartsWith(CHAR ARRAY text,sub)
BYTE pos
 
pos=FindS(text,sub,1)
IF pos=1 THEN
RETURN (1)
FI
RETURN (0)
 
BYTE FUNC EndsWith(CHAR ARRAY text,sub)
BYTE pos,start
 
IF sub(0)>text(0) THEN
RETURN (0)
FI
start=text(0)-sub(0)+1
pos=FindS(text,sub,start)
IF pos=start THEN
RETURN (1)
FI
RETURN (0)
 
BYTE FUNC Contains(CHAR ARRAY text,sub
BYTE ARRAY positions)
BYTE pos,count
 
pos=1 count=0
WHILE pos<=text(0)
DO
pos=FindS(text,sub,pos)
IF pos>0 THEN
positions(count)=pos
count==+1
pos==+1
ELSE
EXIT
FI
OD
RETURN (count)
 
PROC TestStartsWith(CHAR ARRAY text,sub)
IF StartsWith(text,sub) THEN
PrintF("""%S"" starts with ""%S"".%E",text,sub)
ELSE
PrintF("""%S"" does not start with ""%S"".%E",text,sub)
FI
RETURN
 
PROC TestEndsWith(CHAR ARRAY text,sub)
IF EndsWith(text,sub) THEN
PrintF("""%S"" ends with ""%S"".%E",text,sub)
ELSE
PrintF("""%S"" does not end with ""%S"".%E",text,sub)
FI
RETURN
 
PROC TestContains(CHAR ARRAY text,sub)
BYTE ARRAY positions(20)
BYTE i,count
 
count=Contains(text,sub,positions)
IF count>0 THEN
PrintF("""%S"" contains %B ""%S"" at positions:",text,count,sub)
FOR i=0 TO count-1
DO
PrintB(positions(i))
IF i<count-1 THEN
Print(", ")
ELSE
PrintE(".")
FI
OD
ELSE
PrintF("""%S"" does not contain ""%S"".%E",text,sub)
FI
RETURN
 
PROC Main()
TestStartsWith("1234abc","123")
TestStartsWith("1234abc","234")
PutE()
TestContains("abbaabab","ab")
TestContains("abbaabab","ba")
TestContains("abbaabab","xyz")
PutE()
TestEndsWith("1234abc","abc")
TestEndsWith("1234abc","ab")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/String_matching.png Screenshot from Atari 8-bit computer]
<pre>
"1234abc" starts with "123".
"1234abc" does not start with "234".
 
"abbaabab" contains 3 "ab" at positions:1, 5, 7.
"abbaabab" contains 2 "ba" at positions:3, 6.
"abbaabab" does not contain "xyz".
 
"1234abc" ends with "abc".
"1234abc" does not end with "ab".
</pre>
 
=={{header|Ada}}==
<lang Ada>
Anonymous user