// Given a string, compute recursively (no loops) the number of times
// lowercase “hi” appears in the string.
//
// countHi(“xxhixx”) → 1
// countHi(“xhixhix”) → 2
// countHi(“hi”) → 1
#include <stdio.h>
// To enable debug messages uncomment #define
#define TEST 1
int countHi(char *s);
void startTesting();
int main(void) {
#ifdef TEST
startTesting();
#endif
return 0;
}
int countHi(char *s) {
if (*s == '\0' || *(s+1) == '\0') {
return 0;
} else if (*s == 'h' && *(s+1) == 'i') {
return (1 + countHi(s + 2));
} else {
return (countHi(++s));
}
}
void test1()
{
int count = countHi("xxhixx");
printf("countHi(\"xxhixx\") = %d\n", count
); }
void test2()
{
int count = countHi("xhixhix");
printf("countHi(\"xhixhix\") = %d\n", count
); }
void test3()
{
int count = countHi("hi");
printf("countHi(\"hi\") = %d\n", count
); }
void startTesting()
{
test1();
test2();
test3();
}