Challenge
I'm sure you read the title, and came in to farm your rep, thinking its kids' stuff, but think again! You have to challenge each other in the shortest code to count the occurences of a string in another. For example, given the following input:
aaaabbbbsssffhd
as a string, and the string
s
should output
3
Rules
Just before you smile and say, "Hey, I'll use ----," read this:
- No use of external libraries, or your language's API. You have to implement it manually. Which means you can't use your language's built-in function or method for counting occurences
- No file I/O
- No connecting with a server, website, et cetera
- In a case of `ababa`, where it starts with `aba` and if you read the last 3 letters it's also `aba`, you only count one*
Thank you @ProgramFOX for that (the last rule)!
*Hint: When you count occurences, you can remove the ones you counted to avoid disobeying this rule
I think the last 2 rules are just for rule benders!
Winning Criterion
As previously stated the winner is the code with the less bytes used. The winner will be announced five days later (15 June 2014)
My Small Answer
Here's my C++ answer, where it assumes that the li
variable holds the string to check occurences in, and l
is the string to look for in f
:
Ungolfed
int c = 0;
while (li.find(lf) != string::npos)
{
int p = li.find(lf);
int l = p + lf.length() - 1;
for (p = p; p <= l; p++)
{
li[p] = static_cast<char>(8);
}
++c;
}
Of course, to use std::string
, you have to include the string header file!
Golfed
int c=0;while(li.find(lf)!=string::npos){int p=li.find(lf);int l=p+lf.length()-1;for(p=p;p<=l;p++){li[p]=static_cast<char>(8);}++c;}
Result
The variable c
is going to be the value of how many times the string was found
Enjoy!
Winner
After a long wait @Dennis answer wins with only 3 bytes, written in GolfScript
ababa
andaba
, should we output1
or2
? The thirda
is both the end of the firstaba
and the begin of the secondaba
. \$\endgroup\$s
for matching? If not: I have a 5 char J solution:+/a=b
\$\endgroup\$