将任何以 n F 开头或以 f 开头的单词替换为 *

Replace any word that starts with n F or a f with *

本文关键字:开头 单词 替换 任何      更新时间:2023-10-16

问题:编写一个程序,读取一个名为"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,而不是以fF开头

因为您尚未发布 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();
}