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

时间:2015-04-15 03:46:12

标签: c++

问题: 编写一个程序,读取一个名为“naughty.txt”的文本文件并创建另一个名为“nice.txt”的文件,它完全相同,除了一件事:以字母F或f开头的每个单词都被一个字符串替换与移除的单词长度相同的星星。

注意!我们的univesity创建了自己的库,允许我们基本上只使用“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);
}

问题在于我的文件名为naughty.txt,其中包含“我们不喜欢油炸泡菜或油炸任何东西。”

我的nice.txt文件正在输出“wedon'tlikefriendpicklesorfriedanything”

第三编辑:     #include“library.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”中的数据是我们不喜欢*****炒咸菜或任何东西*****炒

2 个答案:

答案 0 :(得分:1)

                if(array[i].words == "f" || array[i].words == "F") 

这会检查单词 f还是F,而不是是否 fF }。

答案 1 :(得分:0)

因为您尚未发布library.h,所以以下示例仅使用标准库。我添加了一些注释来澄清char-by-char读取的想法(不是你期望的逐字逐句):

#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();
}