Text censorer Learn programming C#

Lesson:

File Management


Exercise:

Text censorer


Objetive:

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.


Code:

using System;
using System.IO;
class TextCensorer
{
    static string[] CreateDictionary(string fileName)
    {
        string[] result;
        StreamReader inputFile;
        string line;
        int numLines = 0;

        if (File.Exists(fileName))
        {
            try
            {
                // First: count lines
                inputFile = File.OpenText(fileName);
                do
                {
                    line = inputFile.ReadLine();
                    if (line != null)
                        numLines++;
                } while (line != null);
                inputFile.Close();
                result = new string[numLines];

                // Then: store lines
                inputFile = File.OpenText(fileName);
                int currentLineNumber = 0;
                do
                {
                    line = inputFile.ReadLine();
                    if (line != null)
                    {
                        result[currentLineNumber] = line;
                        currentLineNumber++;
                    }
                } while (line != null);
                inputFile.Close();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
        return null;
    }

    static void Main(string[] args)
    {
        StreamReader inputFile;
        StreamWriter outputFile;
        string line;
        string name;

        if (args.Length < 1)
        {
            Console.WriteLine("Not enough parameters!");
            Console.Write("Enter file name: ");
            name = Console.ReadLine();
        }
        else
            name = args[0];

        if (!File.Exists(name))
        {
            Console.WriteLine("File not found!");
            return;
        }

        try
        {
            string[] wordsList = CreateDictionary("dictionary.txt");
            inputFile = File.OpenText(name);
            outputFile = File.CreateText(name + ".censored");

            do
            {
                line = inputFile.ReadLine();
                if (line != null)
                {
                    // For each line, we must replace any censored word
                    foreach (string censoredWord in wordsList)
                    {
                        if (line.Contains(" " + censoredWord + " "))
                            line = line.Replace(
                                " " + censoredWord + " ",
                                " [CENSORED] ");

                        if (line.Contains(" " + censoredWord + "."))
                            line = line.Replace(
                                " " + censoredWord + ".",
                                " [CENSORED].");

                        if (line.Contains(" " + censoredWord + ","))
                            line = line.Replace(
                                " " + censoredWord + ",",
                                " [CENSORED],");

                        if (line.EndsWith(" " + censoredWord))
                        {
                            line = line.Substring(0,
                                line.LastIndexOf(" " + censoredWord));
                            line += " [CENSORED]";
                        }

                    }
                    // Finally, changes are saved
                    outputFile.WriteLine(line);
                }

            } while (line != null);

            inputFile.Close();
            outputFile.Close();
        }
        catch (PathTooLongException)
        {
            Console.WriteLine("Entered path was too long");
            return;
        }
        catch (IOException ex)
        {
            Console.WriteLine("Input/output error: {0}", ex.Message);
            return;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected error: {0}", ex.Message);
            return;
        }

    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.