Text Censorer - C# Programming Exercise

This exercise involves creating a program to censor text files. The program should read a text file and output its contents to a new file, replacing certain words with "[CENSORED]". The words to censor will be stored in a second data file, a text file that will contain one word per line. The program will need to read this file of censored words and search for each one in the original text file, replacing it with the designated text.

 Category

File Management

 Exercise

Text Censorer

 Objective

Create a program to censor text files. It should read a text file and dump its results to a new text file, replacing certain words with "[CENSORED]". The words to censor will be stored in a second data file, a text file that will contain one word per line.

 Write Your C# Exercise

// Importing necessary namespaces
using System;
using System.Collections.Generic;
using System.IO;

class TextCensorship
{
    // Main method where the program execution begins
    static void Main()
    {
        // Path to the input text file that will be censored
        string inputFilePath = "input.txt";
        
        // Path to the file containing the words to be censored (one word per line)
        string censorFilePath = "censorship_list.txt";
        
        // Path to the output file where the censored text will be written
        string outputFilePath = "output.txt";
        
        // Call the CensorText method to process the file and censor the words
        CensorText(inputFilePath, censorFilePath, outputFilePath);
    }

    // Method to read the input file, censor words, and write the result to a new file
    static void CensorText(string inputFilePath, string censorFilePath, string outputFilePath)
    {
        try
        {
            // Read the words to censor from the censorship list file
            HashSet wordsToCensor = new HashSet(File.ReadAllLines(censorFilePath));

            // Read the input text file into a string
            string inputText = File.ReadAllText(inputFilePath);

            // Loop through each word to censor and replace it with "[CENSORED]"
            foreach (var word in wordsToCensor)
            {
                // Replace all occurrences of the word (case-insensitive)
                inputText = inputText.Replace(word, "[CENSORED]", StringComparison.OrdinalIgnoreCase);
            }

            // Write the censored text to the output file
            File.WriteAllText(outputFilePath, inputText);

            Console.WriteLine("Text has been censored and written to the output file.");
        }
        catch (Exception ex)
        {
            // Catch any errors (e.g., file not found or read/write issues) and display an error message
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  SQL to text

    In this exercise, you need to create a C# program capable of parsing SQL INSERT commands and extracting their data into separate lines of text. The program should pro...

  •  PGM viewer

    The PGM format is one of the versions of the NetPBM image formats. Specifically, it is the variant capable of handling images in shades of gray. Its header starts with a line conta...

  •  Display BMP on console V2

    In this exercise, you are asked to develop a program in C# that can display a 72x24 BMP file on the console. To do this, you must use the information contained...

  •  Writing to a text file

    In this exercise of C#, you need to create a program that asks the user for several sentences (until they just press Enter without typing anything) and stores those s...

  •  Appending to a text file

    In this exercise of C#, you need to create a program that asks the user for several sentences (until they just press Enter without typing anything) and stores those s...

  •  Display file contents

    In this exercise of C#, you need to create a program that displays all the contents of a text file on the screen. The file name will either be entered via the command...