Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
110
Task/String-matching/Action-/string-matching.action
Normal file
110
Task/String-matching/Action-/string-matching.action
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue