統計給定字符串在指定字符串中出現的次數(C++源代碼)
1
//#include <iostream>
2
//統計substr在str中出現的次數并返回
3
int 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
}
這是今天參考網上一個人的代碼修改完善的。

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

posted on 2007-12-05 18:26 浩 閱讀(1404) 評論(0) 編輯 收藏 所屬分類: My Program