RosettaCodeData/Task/String-matching/Picat/string-matching.picat

55 lines
1.3 KiB
Plaintext

import util.
go =>
S1 = "string second",
S2 = "string",
% - Determining if the first string starts with second string
println("Using find/4"),
if find(S1,S2,1,_) then
println("S1 starts with S2")
else
println("S1 does not start with S2")
end,
println("Using append/3"),
if append(S2,_,S1) then
println("S1 starts with S2")
else
println("S1 does not start with S2")
end,
% - Determining if the first string contains the second string at any location
S3 = "this is a string",
S4 = "is a",
if find(S3,S4,Start4,End4) then
printf("S3 contains S4 at pos %d..%d\n", Start4,End4)
else
println("S3 does not contain S4")
end,
% - Determining if the first string ends with the second string
S5 = "this is a string",
S6 = "string",
if find(S5,S6,Start6,S5.length) then
printf("S5 ends with S6, startpos: %d\n",Start6)
else
println("S5 does not end with S6")
end,
if append(_,S6,S5) then
println("S5 ends with S6")
else
println("S5 does not end with S6")
end,
S7 = "this is a string or a string",
S8 = "a string",
All = findall([Start8,End8], find(S7,S8,Start8,End8)),
println(positions=All),
% searching for " "
All2 = findall([Start9,End9], find(S7," ",Start9,End9)),
println(positions=All2),
nl.