Count occurrences of a substring: Difference between revisions
added perl and python solutions |
→Tcl: Added implementation |
||
Line 2: | Line 2: | ||
The task is to either create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments: the first argument being the string to search and the second a substring to be search for. It should return an integer count. |
The task is to either create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments: the first argument being the string to search and the second a substring to be search for. It should return an integer count. |
||
<lang pseudocode> |
<lang pseudocode>print countSubstring("the three truths","th") |
||
3 |
3 |
||
// do not count substrings that overlap with previously-counted substrings: |
// do not count substrings that overlap with previously-counted substrings: |
||
print countSubstring("ababababab","abab") |
|||
⚫ | |||
2 |
|||
⚫ | |||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 20: | Line 19: | ||
print countSubstring("the three truths","th"), "\n"; |
print countSubstring("the three truths","th"), "\n"; |
||
print countSubstring("ababababab","abab"), "\n"; |
print countSubstring("ababababab","abab"), "\n";</lang> |
||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 28: | Line 26: | ||
>>> "ababababab".count("abab") |
>>> "ababababab".count("abab") |
||
2</lang> |
2</lang> |
||
=={{header|Tcl}}== |
|||
The regular expression engine is ideal for matching this, especially as the <tt>***=</tt> prefix makes it interpret the rest of the argument as a literal string to match: |
|||
<lang tcl>proc countSubstrings {haystack needle} { |
|||
regexp -all ***=$needle $haystack |
|||
} |
|||
puts [countSubstrings "the three truths" "th"] |
|||
puts [countSubstrings "ababababab" "abab"]</lang> |
|||
Output:<pre>3 |
|||
2</pre> |
Revision as of 10:20, 16 June 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
The task is to either create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments: the first argument being the string to search and the second a substring to be search for. It should return an integer count.
<lang pseudocode>print countSubstring("the three truths","th") 3
// do not count substrings that overlap with previously-counted substrings: print countSubstring("ababababab","abab") 2</lang>
Perl
This solution uses regex, hence the substring cannot contain regex metacharacters <lang perl>sub countSubstring {
my ($str, $sub) = @_; my $count = () = $str =~ /$sub/g; return $count;
- or return scalar( () = $str =~ /$sub/g );
}
print countSubstring("the three truths","th"), "\n"; print countSubstring("ababababab","abab"), "\n";</lang>
Python
<lang python>>>> "the three truths".count("th") 3 >>> "ababababab".count("abab") 2</lang>
Tcl
The regular expression engine is ideal for matching this, especially as the ***= prefix makes it interpret the rest of the argument as a literal string to match: <lang tcl>proc countSubstrings {haystack needle} {
regexp -all ***=$needle $haystack
} puts [countSubstrings "the three truths" "th"] puts [countSubstrings "ababababab" "abab"]</lang>
Output:
3 2