хз. может быть дело в кодировке. вот если читать бинарно файлы, у меня выходит:
str size = 5779
templ size = 2
templates count = 14
регистр при поиске учитывается. т.е. Эт искать не будет. только эт
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| #include <iostream>
#include <string>
#include <fstream>
#include <iterator>
std::size_t GetTemplatesCountInString(std::string str, const std::string& templ)
{
if (str.empty() || templ.empty())
{
return 0;
}
std::size_t count = 0;
std::string::size_type offset = 0;
while (true)
{
const std::string::size_type templPos = str.find(templ, offset);
if (templPos == std::string::npos)
{
break;
}
++count;
offset = templPos + templ.size();
if (offset > str.size())
{
break;
}
}
return count;
}
std::string ReadDataFromFile(const std::string& filePath)
{
std::ifstream file;
file.exceptions(std::ios::badbit | std::ios::failbit);
file.open(filePath, std::ios::in | std::ios::binary);
return std::string((std::istreambuf_iterator<char>( file ) ), std::istreambuf_iterator<char>());
}
int main()
{
const std::string str = ReadDataFromFile("d:\\tmp\\Kniga1.txt");
const std::string templ = ReadDataFromFile("d:\\tmp\\Podstroka.txt");
std::cout << "str size = " << str.size() << std::endl;
std::cout << "templ size = " << templ.size() << std::endl;
std::cout << "templates count = " << GetTemplatesCountInString(str, templ) << std::endl;
return 0;
} |
|
Добавлено через 4 минуты
для учета регистра начинается совсем другая история. там конвертить в юникод нужно или с локалями колдовать. в общем не английским тестом придется помучится. оставляю это вам