Find Number of Occurrences

How will you find the number of occurrences of a particular string in the given input string?

Questions by rolemodel2k7

Showing Answers 1 - 6 of 6 Answers

printfbabu

  • Dec 28th, 2009
 

int computeSubStrings(const char *string,const char *substr)

{
int count = 0;
char *token = strstr(string,substr);
while(token != NULL)
{
count++;
token = strstr(token + 1,substr);
}
return count;
}

The above function can be used for counting no. of occurences of a substring in a string

kbjarnason

  • Jul 1st, 2010
 

The question is insufficiently specified.  For example:

  "Find the number of occurrences of NN in the string NNNNN"

One answer would be 2 occurrences - the first NN pair, the second NN pair, and the final N doesn't match.

A different answer would be 4 occurrences; the first and second N, the second and third N, the third and fourth N, the fourth and fifth N.

Without properly specifying the meaning of "number of occurrences", either result would be correct.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions