统计一个文本文件中某个字符串出现的次数,把该字符串的出现次数及出现的各个位置输出到显示器
时间: 2023-11-09 14:06:58 浏览: 32
以下是 Python 代码实现:
```python
with open('file.txt', 'r') as f:
content = f.read()
target_str = 'target' # 待统计的字符串
count = 0
positions = []
start_pos = 0
while True:
pos = content.find(target_str, start_pos)
if pos == -1:
break
count += 1
positions.append(pos)
start_pos = pos + len(target_str)
print(f'出现次数:{count}')
print(f'出现位置:{positions}')
```
其中 `file.txt` 是待统计的文本文件,`target_str` 是待统计的字符串。
首先使用 `with open()` 打开文件,并将读取到的内容存储到 `content` 变量中。
然后使用 `find()` 方法循环查找 `target_str` 在 `content` 中的位置,每找到一个位置就将计数器 `count` 加 1,同时将该位置加入到 `positions` 列表中。循环结束后,输出结果即可。
相关问题
C++实现统计一个文本文件中某个字符串出现的次数,把该字符串的出现次数及出现的各个位置输出到显示器
可以使用C++中的fstream库来读取文本文件,然后使用字符串查找函数来统计某个字符串出现的次数并输出出现的位置。
以下是一个示例代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string filename, search_str;
cout << "Enter filename: ";
cin >> filename;
cout << "Enter search string: ";
cin >> search_str;
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file!" << endl;
return 1;
}
string line;
int count = 0;
while (getline(file, line)) {
size_t pos = line.find(search_str);
while (pos != string::npos) {
count++;
cout << "Found at position " << pos << endl;
pos = line.find(search_str, pos + 1);
}
}
cout << "Total count: " << count << endl;
return 0;
}
```
该程序首先要求用户输入文件名和要查找的字符串,然后打开文件进行逐行读取。对于每一行,使用字符串查找函数`find()`查找字符串出现的位置并输出,直到该行中没有该字符串。同时,统计该字符串出现的总次数并输出。
用c++编程,统计一个文本文件中某个字符串的出现次数,把该字符串的出现次数及出现的各个位置输出到显示器,并逐句解释代码
以下是一个简单的C++程序,可以统计文本文件中某个字符串的出现次数并输出出现位置:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string file_name = "test.txt"; // 文本文件名
string target_str = "hello"; // 目标字符串
vector<int> positions; // 记录目标字符串出现位置的向量
int count = 0; // 记录目标字符串出现次数的计数器
// 打开文本文件
ifstream infile(file_name);
if (!infile.is_open())
{
cout << "无法打开文件" << endl;
return -1;
}
// 逐行读取文本文件
string line;
int line_num = 1; // 记录当前行号
while (getline(infile, line))
{
// 在当前行查找目标字符串
size_t pos = line.find(target_str);
while (pos != string::npos)
{
// 记录目标字符串出现位置
positions.push_back(pos + line_num * (target_str.length() + 1));
count++;
// 继续在当前行查找目标字符串
pos = line.find(target_str, pos + 1);
}
// 增加行号
line_num++;
}
// 输出统计结果
cout << "出现次数:" << count << endl;
cout << "出现位置:";
for (int i = 0; i < positions.size(); i++)
{
cout << positions[i];
if (i < positions.size() - 1)
cout << ", ";
}
cout << endl;
// 关闭文本文件
infile.close();
return 0;
}
```
代码解释:
1. 第1行到第5行是头文件和命名空间声明。
2. 第7行到第10行定义了程序的入口函数 `main()`,并声明了需要用到的变量。
3. 第12行和第13行分别指定了要读取的文本文件名和要查找的目标字符串。
4. 第14行定义了一个 `vector` 类型的变量 `positions`,用于记录目标字符串出现的位置。
5. 第15行定义了一个整型变量 `count`,用于记录目标字符串出现的次数。
6. 第18行到第27行打开文本文件并逐行读取其中的内容,通过 `string::find()` 函数查找目标字符串在当前行中的位置,并将位置记录在 `positions` 向量中。如果目标字符串在当前行中出现了多次,需要多次查找并记录位置。这里使用了 `while` 循环来实现。
7. 第29行到第38行输出统计结果。其中,第31行输出了目标字符串出现的次数,第32行输出了目标字符串出现的位置,注意要在相邻的位置之间加上逗号,方便查看结果。
8. 第41行关闭文本文件。
9. 最后,程序返回值为0,表示正常结束。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)