115k views
5 votes
Assume s is a string of lower case characters.

Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print

Number of times bob occurs is: 2

1 Answer

4 votes
Here's an attempt in C:

#include <string.h>

int CountBob(const char * s)
{
int n = 0;
while (s = strstr(s, "bob")) {
n++;
s++;
}
return n;
}

int main()
{
char *s = "azcbobobegghakl";
int n = CountBob(s);
printf("Number of times bob occurs is: %d", n);
getchar();
}


User JeffSahol
by
6.7k points