这个就是遍历字符串,每次相同的时候计数器加1。程序如下:
int mychcount(char *S, char ch){ int count = 0; while (*S != '\0') { if (*S == ch) count++; S++; } return count;}
测试程序如下:
void testcode24(){ char *S = "This string is used to test the function"; cout << S << endl; char ch = 's'; cout << mychcount(S, ch) << endl;}
结果如图: