您的位置:首页 > 其它

统计字符串中有‘0’——‘9’的字符个数

2016-07-15 15:33 281 查看
#include<iostream>
using namespace std;
int Cllcdigital(char *str)
{
if(NULL == str){
return 0;
}
int i = 0;
int size = 0;
int count[256] ={0};
char *sd = NULL;
sd = str;
while(*sd != '\0'){

count[*sd] = count[*sd] +1;
sd++;
}
for(i = '0';i<= '9';i++)
{
size += count[i];
}
return size;
}
int main()
{
int size = 0;
char str[] = "he1234llo,0909";
size = Cllcdigital(str);
cout << size <<endl;
return 0;
}
运行结果:

8
Press any key to continue


体会:

有点类似哈希算法的思想,想象有256个桶 ,

来一个元素,就将它放入桶中,每个桶中加一

就可求出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: