// L-36 MCS 360 Mon 20 Nov 2017 : charcount.cpp

/*
  Give a recursive definition of a function CharCount
  which takes on input a string s and a character c.
  The function returns the number of times the character
  c occurs in the string s.  You must use recursion.
 */

#include <iostream>
#include <string>

using namespace std;

int CharCount ( string s, char c, int k );
// Returns the number of occurrences of c in s,
// starting at position k.

int main ( void )
{
   cout << "Give a word : ";
   string word; cin >> word;

   cout << "Give a letter : ";
   char letter; cin >> letter;

   int count = CharCount(word, letter, 0);
   cout << "The letter " << letter << " occurs "
        << count << " times in " << word << "." << endl;

   return 0;
}

int CharCount ( string s, char c, int k )
{
   if(k > s.size())
      return 0;
   else if(s[k] == c)
      return 1 + CharCount(s, c, k+1);
   else
      return CharCount(s, c, k+1);
}
