In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of April 7th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #05 exercise sheet]


In this problem you should submit a file containing the function as described (without any main function). Inside the function do not print anything that was not asked!

[PII038] Whispers in the Shadows

In the twisting corridors of King’s Landing, knowledge is power — and no one knows this better than Lord Varys, the Master of Whisperers. His network of spies, known as "little birds", brings him secrets from every corner of the realm. But even a spider needs help untangling the web of words.

One night, in a candlelit chamber, Varys summons you and presents a challenge: "Words can topple kings and raise the lowborn. Find how often a certain word appears in this text. Do this well, and perhaps I shall share a secret of my own."

The Problem

Write a function int occurrences(char key[], char str[]) that, given two strings key and str, returns the number of occurrences of key within str. Each occurrence is made by contiguous characters and different occurrences can overlap.

For example, the key "ana" appears 4 times in "banana_ananas": "banana_ananas", "banana_ananas", "banana_ananas", "banana_ananas".

Constraints

The following limits are guaranteed in all the test cases that will be given to your program:

1 ≤ |key| < 100       Length of the key
1 ≤ |str| < 100       Length of the string

Submission

You should submit a .c file containing the requested function, without any main function and without printing anything. You can however create additional methods, if you need them.

Mooshak will use the following code to link to your function, read the inputs and call your method, printing its result.

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100

int occurrences(char [], char []);

int main(void) {

  // Read amount of test cases
  int n;
  scanf("%d", &n);

  // Read n cases and for each call the ocurrences function
  char key[MAX_SIZE], str[MAX_SIZE];
  for (int i=0; i<n; i++) {
    scanf("%s %s", key, str);
    printf("occurrences(\"%s\",\"%s\") = %d\n", key, str, occurrences(key, str));
  }
  
  return 0; 
}

Example Input Example Output
3
stark robb_stark_and_sansa_stark_are_stark_brothers
snow winter_is_coming
ana banana_ananas
occurrences("stark","robb_stark_and_sansa_stark_are_stark_brothers") = 3
occurrences("snow","winter_is_coming") = 0
occurrences("ana","banana_ananas") = 4

Programming II (CCINF1002)
DCC/FCUP - University of Porto