// Arup Guha
// 4/29/2022
// Solution to Spring 2022 COP 3502 Final Exam Question 6

#include <stdio.h>

int numOccurrences(char* str, char target);

int main(void) {

    // Some tests.
    printf("%d\n", numOccurrences("hello", 'l'));
    printf("%d\n", numOccurrences("thisisasillystring", 's'));
    printf("%d\n", numOccurrences("thisisasillystring", 'q'));
    return 0;
}

// Returns the number of times target occurs in str.
int numOccurrences(char* str, char target) {

    // End of string, so 0.
    if (str[0] == '\0') return 0;

    // Recursively call it on the rest of the string and add 1 iff the first letter is target.
    return (str[0] == target) + numOccurrences(str+1, target);
}
