无题

拿个学位是骗自己的。学问是一辈子的。

统计

留言簿(3)

阅读排行榜

评论排行榜

统计给定字符串在指定字符串中出现的次数(C++源代码)

 1//#include <iostream>
 2//统计substr在str中出现的次数并返回
 3int CountSubString(std::string const& str, std::string const& substr){
 4  int nCount = 0;
 5  std::string::size_type substrSize = substr.size();
 6  std::string::size_type IndexOfFind = 0;
 7  IndexOfFind = str.find(substr, IndexOfFind);
 8  while (IndexOfFind!=std::string::npos) {
 9    ++nCount;
10    if (substrSize>1){
11      IndexOfFind+=substrSize;
12    }

13    else{
14      ++IndexOfFind;
15    }

16    IndexOfFind = str.find(substr, IndexOfFind);
17  }

18  return nCount;
19}
这是今天参考网上一个人的代码修改完善的。

posted on 2007-12-05 18:26 阅读(1379) 评论(0)  编辑  收藏 所属分类: My Program


只有注册用户登录后才能发表评论。


网站导航: