package sut;      // update the package to match yours

// Authors: Paul Ammann & Jeff Offutt
// Supriya Savaram & Upsorn Praphamontripong, Modified for CS 3250, graph based testing homework 

/** *****************************************************
// repeatedWords checks for repeat words. It prints a list of repeat words, by line number.
// repeatedWords will accept standard input.
 *  Jeff Offutt, June 1989 (in C), Java version March 2003 
********************************************************* */
import java.io.*;

public class repeatedWords
{
   // Class variables used in multiple methods.
   private boolean lastdelimit = true;
   private String curWord = "", prevWord = "";
   private char delimits [] =
           {'	', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?',
            '&', '{', '}', '\\'};    // First char in list is a tab
  
   private String result = "";  
   
   public repeatedWords () 
   {
      result = "";
   } 
   
   //************************************************
   // repeat() reads all lines in the string input, 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.
   //************************************************
   public void repeat(String str_input) throws IOException
   {
      char c;
      int linecnt = 1;
      
      String[] lines = str_input.trim().split("\\r?\\n");
      
      while (linecnt <= lines.length)
      {
         String line = lines[linecnt-1];
         for (int i=0; i<line.length(); i++)
         {            
            c = line.charAt(i);      // for each character
            if (isDelimit (c))
            {  // Found an end of a word.
               checkDupes (linecnt);
            }
            else
            {
               lastdelimit = false;
               curWord = curWord + c;
            }                        
         }
         checkDupes(linecnt);
         linecnt++;
      }    	       
   }  
   
   //************************************************
   // checkDupes() checks to see if the globally defined curWord is the same as prevWord
   // and prints a message if they are the same.
   //************************************************
   private boolean checkDupes (int line)
   {
      if (lastdelimit)
         return false;       // already checked, keep skipping
      
      lastdelimit = true;
      if (curWord.equals(prevWord))
      {
         result += "Repeated word on line " + line + ": " + prevWord+ " " + curWord + "\n";
         return true;
      }
      else
         prevWord = curWord;

      curWord = "";
      return false;
   }  
   
   //************************************************
   // Checks to see if a character is a delimiter.
   //************************************************
   private boolean isDelimit (char C)
   {
      for (int i = 0; i < delimits.length; i++)
         if (C == delimits [i])
            return true;
      return false;
   }
   
   //************************************************
   // Get report
   //************************************************ 
   public String getReport()
   {
      return result;
   }
   
} 