/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; using System.IO; using System.Text; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 2, section 2.5, page 80 /// /// ***************************************************** /// // Stutter checks for repeat words in a text file. /// // It prints a list of repeat words, by line number. /// // Stutter will accept standard input or a list /// // of file names. /// Jeff Offutt, June 1989 (in C), Java version March 2003 /// ********************************************************* /// internal class Stutter { // Class variables used in multiple methods. private static bool lastdelimit = true; private static string curWord = "", prevWord = ""; private static char[] delimits = new char[] { ' ', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\' }; //************************************************ // main parses the arguments, decides if stdin // or a file name, and calls Stut(). //************************************************ //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static void main(String[] args) throws IOException public static void Main(string[] args) { string fileName; StreamReader myFile; StreamReader inFile = null; if (args.Length == 0) { // no file, use stdin inFile = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(Console.ReadLine()))); } else { fileName = args [0]; if (fileName == null) { // no file name, use stdin inFile = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(Console.ReadLine()))); } else { // file name, open the file. myFile = new StreamReader(fileName); inFile = myFile; } } stut(inFile); } //************************************************ // Stut() reads all lines in the input stream, and // finds words. Words are defined as being surrounded // by delimiters as defined in the delimits[] array. // Every time an end of word is found, checkDupes() // is called to see if it is the same as the // previous word. //************************************************ //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private static void stut(BufferedReader inFile) throws IOException private static void stut(StreamReader inFile) { try { string inLine; char c; int linecnt = 1; while ((inLine = inFile.ReadLine()) != null) { // For each line for (int i = 0; i < inLine.Length; i++) { // for each character c = inLine[i]; if (isDelimit(c)) { // Found an end of a word. checkDupes(linecnt); } else { lastdelimit = false; curWord = curWord + c; } } checkDupes(linecnt); linecnt++; } } catch (Exception Ex) { Console.WriteLine(Ex.ToString()); } } // end Stut //************************************************ // checkDupes() checks to see if the globally defined // curWord is the same as prevWord and prints a message // if they are the same. //************************************************ private static void checkDupes(int line) { if (lastdelimit) { return; // already checked, keep skipping } lastdelimit = true; if (curWord.Equals(prevWord)) { Console.WriteLine("Repeated word on line " + line + ": " + prevWord + " " + curWord); } else { prevWord = curWord; } curWord = ""; } // end checkDupes //************************************************ // Checks to see if a character is a delimiter. //************************************************ private static bool isDelimit(char C) { for (int i = 0; i < delimits.Length; i++) { if (C == delimits[i]) { return (true); } } return (false); } } // end class Stutter