/* String matching (Rosetta code) in Picat. http://rosettacode.org/wiki/String_matching """ Given two strings, demonstrate the following 3 types of matchings: - Determining if the first string starts with second string - Determining if the first string contains the second string at any location - Determining if the first string ends with the second string Optional requirements: - Print the location of the match for part 2 - Handle multiple occurrences of a string for part 2. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => S1 = "string second", S2 = "string", % - Determining if the first string starts with second string % using find println("Using find/4"), if find(S1,S2,1,_) then println("S1 starts with S2") else println("S1 does not start with S2") end, % using append 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.