Proposed exercise
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.
Output
Solution
using System.IO; using System; class TextCensorer { static void Main() { string name = Console.ReadLine(); try { string[] linesFile = ReadFile("dictionary.txt"); StreamReader file = File.OpenText(name); StreamWriter fileCensored = File.CreateText(name + ".censored"); string line; do { line = file.ReadLine(); if (line != null) { foreach(string line in linesFile) { if (line.Contains(" " + line + " ")) line = line.Replace(" " + line + " ", " [CENSORED] "); if (line.Contains(" " + line + ".")) line = line.Replace(" " + line + ".", " [CENSORED]."); if (line.Contains(" " + line + ",")) line = line.Replace(" " + line + ",", " [CENSORED],"); if (line.EndsWith(" " + line)) { line = line.Substring(0, line.LastIndexOf(" " + line)); line += " [CENSORED]"; } } fileCensored.WriteLine(line); } } while(line != null); file.Close(); fileCensored.Close(); } catch (Exception) { Console.WriteLine("Error"); return; } } static string[] ReadFile(string name) { try { StreamReader file = File.OpenText(name); string line; int lines = 0; do { line = file.ReadLine(); if (line != null) lines++; } while(line != null); file.Close(); string[] fileLines = new string[lines]; file = File.OpenText(name); int c = 0; do { line = file.ReadLine(); if (line != null) { fileLines[c] = line; c++; } } while(line != null); file.Close(); return fileLines; } catch (Exception) { return null; } return null; } }