编制函数mychcount来统计某个字符 ch在一个字符串S中出现的次数

编制函数mychcount来统计某个字符 ch在一个字符串S中出现的次数C程

这个就是遍历字符串,每次相同的时候计数器加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;}

结果如图:

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答