Count occurrences of a substring: Difference between revisions
Content added Content deleted
(Added Kotlin) |
|||
Line 1,291: | Line 1,291: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Solution 1: |
|||
⚫ | |||
⚫ | |||
return select(2, s1:gsub(s2, "")) |
|||
end |
|||
print(countSubstring("the three truths", "th")) |
|||
print(countSubstring("ababababab", "abab"))</lang> |
|||
<pre>3 |
|||
2</pre> |
|||
Solution 2: |
|||
<lang Lua>function countSubstring(s1, s2) |
|||
local count = 0 |
local count = 0 |
||
for eachMatch in s1:gmatch(s2) do |
for eachMatch in s1:gmatch(s2) do |
||
count = count + 1 |
|||
end |
|||
return count |
return count |
||
end |
end |
||
print(countSubstring("the three truths", "th")) |
print(countSubstring("the three truths", "th")) |
||
print(countSubstring("ababababab","abab"))</lang> |
print(countSubstring("ababababab", "abab"))</lang> |
||
<pre>3 |
<pre>3 |
||
2</pre> |
2</pre> |