将任何以 n F 开头或以 f 开头的单词替换为 *
Replace any word that starts with n F or a f with *
问题:编写一个程序,读取一个名为"naughty.txt"的文本文件,并创建另一个名为"nice.txt"的文件,该文件完全相同,除了一件事:每个以字母F或f开头的单词都被一串与删除的单词长度相同的星星替换。
注意!我们的大学创建了自己的库,允许我们基本上只使用"library.h"并能够编码。
我的代码:
#include "library.h"
struct mytype{string word;};
void replace(mytype array[], int size)
{
ifstream fin("naughty.txt");
if(fin.fail())
{
cout << "Error opening file "<< endl;
exit(1);
}
else {
while(!fin.eof())
{
for(int i = 0; i < size; i++)
{
if(array[i].word =="f" || array[i + 1].word == "F ")
固定代码:
#include "library.h"
struct mytype{string words;};
void read(mytype array[], int size)
{
ifstream fin("naughty.txt.txt");
if(fin.fail())
{
cout << "Input file didn't open." << endl;
}
else
{
ofstream fout("nice.txt");
for(int i = 0; i < size; i++)
{
fin >> array[i].words;
if(array[i].words == "f" || array[i].words == "F")
{
int length = array[i].words.length();
int j = 1;
while(j < length)
{
fout << "*";
j++;
}
}
fout << array[i].words;
}
}
}
void main()
{
int size = 1000;
mytype array[1000];
read(array, size);
}
问题是我的文件叫顽皮.txt里面有这个"我们不喜欢油炸泡菜或油炸任何东西。
我的漂亮.txt文件正在输出"我们不喜欢朋友泡菜或弗里德任何东西"
第三次编辑: #include "图书馆.h"
struct mytype{string words;};
void read(mytype array[], int size)
{
ifstream fin("naughty.txt");
if(fin.fail())
{
cout << "Input file didn't open." << endl;
}
else
{
ofstream fout("nice.txt");
for(int i = 0; i < size; i++)
{
fin >> array[i].words;
if(array[i].words[0] == 'f' || array[i].words[0] == 'F')
{
int length = array[i].words.length();
int j = array[i].words.find_first_of("f" );
while(j < length)
{
fout << "*";
j++;
}
}
else if(i > 0){
fout << ' ';
fout << array[i].words << " ";}
}
}
}
void main()
{
int size = 1000;
mytype array[1000];
read(array, size);
}
文件"nice.txt"中的数据是 我们不喜欢*****油炸泡菜或任何*****油炸的东西
if(array[i].words == "f" || array[i].words == "F")
这将检查单词是f
还是F
,而不是以f
或F
开头。
因为您尚未发布 library.h,所以以下示例仅使用标准库。我添加了一些评论来澄清逐个字符阅读的想法(而不是您所期望的逐字阅读):
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int main(void)
{
// open file streams
ifstream fin; // input stream
ofstream fout; // output stream
char c; // bufer (just one character)
bool word = false; // become true when new word is found
bool replace = false; // become true when f-word is processed
// open and check
fin.open("naughty.txt", ifstream::in);
if( !fin.is_open() )
{
cerr << "ERROR: Source file cannot be open!" << endl;
return 1;
}
fout.open("nice.txt", ofstream::out);
if( !fout.is_open() )
{
cerr << "ERROR: Destiation file cannot be open!" << endl;
return 1;
}
// read and write until end of file reached or reading fail
while (fin.get(c))
{
// check the read character for being not a letter (part of word)
if( !isalpha(c) ) // consider changing condition to: if( isspace(c) )
{ // it is lot a letter, so it is not a word and nothing to be changed
word = false;
replace = false;
// And just repeat this character
fout.put(c);
}
else
{ // it is a letter, so it is a word
if( !word ) // if it is a new word
{
word = true;
// check the first letter
if( c == 'F' || c == 'f' )
{
replace = true; // now replasement is needed
}
}
if( replace )
{ // if replacement is needed
fout.put('*');
}
else
{ // or just repeat the character
fout.put(c);
}
}
}
// close files
fin.close();
fout.close();
}
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何从列表中找到以"b"开头的单词和带有 4 个符号的单词?
- C++ 随机生成的字典单词将仅显示以 B 开头的单词
- 如何输出以姓氏开头的全名?如果名字或姓氏由单词组成,该怎么办?
- 用于获取以元音和辅音开头的单词数量的程序返回错误的答案
- 打印以 "a" 开头的单词(字符串中)
- 使用线程在以元音或辅音开头的单词之间交替
- 将任何以 n F 开头或以 f 开头的单词替换为 *
- 在单词词典中提取以片段开头/包含/结束的单词
- 正则表达式在开头单独匹配单词,或者在输入结束时通过不重复字符串附加到某些内容但附加到某些内容
- 从以特定单词开头的目录中提取所有文件
- C++删除单词开头的非字母字符
- 如何匹配以大写字母和非大写字母开头的单词?
- 如何检查文本文件中的特定行是否以特定单词开头
- 如何检查一个单词是否以大写字母开头,如果是,则设置条件