Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

Text censorer - Tutorial C# Sharp
Tutorial C# Sharp4,85581245

Text censorer - Tutorial C# Sharp


Lesson 8:

File management


Exercise 8.38:

Text censorer


Objetive:

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


Source 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;
        }

    }
}
Exercisey 8.38






Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.