Member Avatar for Stephcain12

Hey, I am doing some programming exercises, it is to read in a txt file and then read in a list of banned words in an array. I've managed to do this but the last step requires me to filter the original text file with the banned words and change them to stars e.g.

one of the banned words is "dog", when this comes up in the textFile1 anywhere that it says dog should change to "***".

ive seen something where you would put textFile1.replace(textFile1.find("dog"), "***" but its not using the array so I dont understand how to filter using the array i made.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;






int main()
{
    fstream textFile1;
    textFile1.open("text1.txt", ios::in);
    if (textFile1.is_open())
    {
        string line;
        while (getline(textFile1, line)) {
            cout << line << endl;
        }
        textFile1.close();
    }




    string bannedWords[100];
    int i = 0;

    ifstream bannedFile("banned.txt");
    if (bannedFile)
    {
        while ((bannedFile >> bannedWords[i]) && (i < 100))
        {
            i++;
        }

    }
    else
    {
        cout << "Unable to open file" << endl;
        exit(0);
    }

    for (i = 0; i < 8; i++) {
        cout << bannedWords[i] << endl;
    }

}
Member Avatar for Stephcain12

i added this to the end, and it censors the first of all the banned words but i need it to go through all the words and find them?

string firstText;
    fstream textFileOne;
    textFileOne.open("text1.txt", ios::in);
    if (textFileOne.is_open())
    {
        while (getline(textFileOne, firstText)) {
            textFileOne >> firstText;
        }
    }

    firstText.replace(firstText.find(bannedWords[0]), 3, "***");
    firstText.replace(firstText.find(bannedWords[1]), 3, "***");
    firstText.replace(firstText.find(bannedWords[2]), 3, "***");




    cout << firstText;

Actually that's pretty close. The lines 11,12,13 is the old brute force thing you might do in a hurry to get it out before lunch.

What you may want is a loop that iterates from 0 to the number of banned words. Something like:
for (i = 0; i < numberofbannedwords; i++) { firstText.replace(firstText.find(bannedWords[1]), 3, "***"); }

Sorry if I don't dive into possible optimizations.

Member Avatar for Stephcain12

i tried this but couldnt get it to work, do i intialise number of banned words with anything specific? sorry im very new to this

So you want to know the number of bannedwords? At the top post you read said file of banned.txt. Now have a variable initialized to zero and there you have at line 36 you increment i. That's your word count.

Sorry but no I have not written anything about the area around line 36 or commented that it doesn't look like you are reading in any words. I leave this incomplete code for you to finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.