@ForEveR
В астрале
7989 / 4748 / 321
Регистрация: 24.06.2010
Сообщений: 10,547
|
27.02.2011, 19:48
|
|
Проверил - работает.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| #include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
char* strtolower(const char* str)
{
char* res=new char[strlen(str)+1];
strcpy(res, str);
for(size_t i=0; i<strlen(res); ++i)
res[i]=tolower(res[i]);
return res;
}
int main()
{
const int len = 255;
char word[len];
char line[len];
char *modline;
char* modword;
setlocale(LC_ALL, "Russian");
while(1)
{
cout << "Введите слово для поиска: ";
if(gets(word)==NULL) goto q;
int l_word = strlen(word);
modword=strtolower(word);
ifstream fin ("text.txt");
if (!fin)
{
cout << "Ошибка открытия файла." << endl;
return 1;
}
int count = 0;
while (!fin.eof()) // заносим строку длинной len в переменную line
{
fin.getline(line, len);
if(!strcmp(line, ""))
break;
modline=strtolower(line); //превращаем все прописные буквы в строчные
char *p = modline; // р - для хранения позиции за найденной подстрокой
while(p=strstr(p, modword)) // многократно ищем вхождение подстроки в строку
{
char *c=p; //c - для хранения адреса начала вхождения подстроки
p+=l_word; // перемещаем указатель на длину заданного слова
if(c!=modline) // слово не в начале строки?
if (!ispunct(*(c-1)) && !isspace(*(c-1))) // символ перед словом не разделитель?
continue;
if (ispunct(*p) || isspace(*p) || (*p == '\0')) // символ после слова разделитель?
count++;
}
}
cout << endl << "Слово " << word << " встретилось в файле " << count << " раз(а). Выход <Ctrl+Z>" << endl << endl;
}
q:;
return 0;
} |
|
1
|