Longest common prefix: Difference between revisions

Add CLU
(Added solution for Action!)
(Add CLU)
Line 1,181:
lcp( "foo" , "foobar" ) = foo
</pre>
 
=={{header|CLU}}==
<lang clu>lcp = proc (strs: ss) returns (string)
ss = sequence[string]
if ss$empty(strs) then return("") end
pfx: string := ss$bottom(strs)
for str: string in ss$elements(strs) do
if string$empty(pfx) then return("") end
if string$size(str) < string$size(pfx) then
pfx := string$substr(pfx, 1, string$size(str))
end
no_match: int := 1
while no_match <= string$size(pfx) cand
str[no_match] = pfx[no_match] do
no_match := no_match + 1
end
pfx := string$substr(pfx, 1, no_match-1)
end
return(pfx)
end lcp
 
start_up = proc ()
ss = sequence[string]
sss = sequence[ss]
po: stream := stream$primary_output()
tests: sss := sss$[
ss$["interspecies","interstellar","interstate"],
ss$["throne","throne"],
ss$["throne","dungeon"],
ss$["throne","","dungeon"],
ss$["cheese"],
ss$[""],
ss$[],
ss$["prefix","suffix"],
ss$["foo","foobar"]
]
for test: ss in sss$elements(tests) do
stream$putl(po, "\"" || lcp(test) || "\"")
end
end start_up</lang>
{{out}}
<pre>"inters"
"throne"
""
""
"cheese"
""
""
""
"foo"</pre>
 
=={{header|D}}==
2,094

edits