创建程序以从给定的.txt文件中查找文本,替换并计算单词
Create program to find a text from given .txt file , replace and count the words
我的老师要求我创建这个程序,但我只知道如何做B任务。 他给了我一个示例文本,并告诉我输入它然后做一些操作(下面(。我查找任务 A,但他说我们需要像这样做代码。 我花了几天时间写《从控制结构到对象C++》一书,这是他课程所需的,但我找不到与"查找字符串"相关的章节。
string text = "The aa Art of Computer Programming";
int pos = 0;
string target = "aa";
pos = text.find(target, 0);
}
但问题是我不知道如何更改pos = text.find(target, 0);
=>pos = "input file ?".find(target,0)
.
a. 搜索文本。程序从用户接收搜索词,搜索文件的内容 并返回是否找到匹配的单词以及匹配的单词数量。
b. 搜索和替换文本。程序从 用户,它将文件内容中的匹配词替换为替换词。
c. 确定字数。
d. 确定字符数(包括空格(
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string strTarget = " "; //String to search
string strNew = " "; //String To re
ifstream filein("test.txt"); //File to read from
ofstream fileout("replaced.txt"); //Temporary file
int pos = 0;
int menuItem = 0;
cout << "What do you want to do today ?" << endl;
cout << "1. Search a text " << endl;
cout << "2. Search and replace the text " << endl;
cout << "3. Determine number of word" << endl;
cout << "4. Determine number of character " << endl;
cin >> menuItem;
switch (menuItem)
{
case 1:// search
system("cls");
cout << "Please enter the text you want to search" << endl;
cin >> strTarget;
break;
case 2: // search and replace
cout << " Tell me the text you want to replace " << endl;
cin >> strTarget;
cout << " So what It's suppose to be ? " << endl;
cin >> strNew;
if (!filein || !fileout) //if both files are not available
{
cout << "Error opening files!" << endl;
return 1;
}
string strTemp;
//bool found = false;
while (filein >> strTemp)//it will check line from test to strTemp string
{
if (strTemp == strTarget)//if your word found then replace
{
strTemp = strNew;
//found = true;
}
strTemp += " ";
fileout << strTemp;//output everything to fileout(temp.txt)
//if(found) break;
}
return 0;
}
}
- 代码的注释不是真的正确,因为我稍微更改了名称。 我知道这对大多数程序员来说有点荒谬,但我是 CS 的新手,而且肯定我对书中的任何代码都有麻烦。请帮助我,而不是粗鲁。非常感谢! 我希望我的大学有CS的导师,但不知何故,他们负担不起CS导师。这就是为什么我需要你们的帮助.
任务 A:计算字符串中出现的字符数
C++任务 B:如何替换字符串中出现的所有字符?
任务C:C++函数来计算字符串中的所有单词
对于最后一个任务,您只需循环访问字符串中的所有字符,直到达到空字符,每次递增一个计数器。
相关文章:
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 如何从文本文件中查找最常见的字母和出现次数?
- 在文本中查找半个单词"ABBA"的问题
- 在C++中查找文本中的模式
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- 如何创建一个函数,该函数在文本中查找单词的匹配项,包括跳过
- 在大型 JSON 文件中查找文本
- 尝试在自定义 QT 创建器文本编辑器中创建查找和替换循环.我似乎无法设置光标位置
- 文本文件中的电话号码查找
- 如何在没有分隔符的大文本文件中查找所有字典单词
- C++查找文本和计数出现次数
- 在文本文件 C++ 中查找( ")
- Visual Studio,在(解决方案资源管理器的)某个筛选器内的所有文件中查找/替换文本
- 查找字符在标准输入文本中出现的次数
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词
- 文本处理以查找字符串的共存
- 使用 TinyXML 查找特定的文本值
- QTreeWidgetItem 通过文本查找子项
- 查找页面上文本的坐标-c++,windows,VS2013
- 如何做一个换行的文本查找器