title: Finding the longest text fragment that exists in both strings
body: <p>I need to find the longest text fragment that exists in both strings and number of the line in which the fragment starts for both strings. In this case I'm storing the text in <code>Book</code> class, because it makes sense for other things I have to do with it. I need a method inside my main class, which would look something like this: </p>
  
  <pre><code>static void FindLongestFragment(Book book1, Book book2, out string fragment, out int lineNumber1, out int lineNumber2)
  </code></pre>
  
  <p>However, I can't think of an algorithm to do this. Here is what my program looks so far:</p>
  
  <pre><code>class Book
  {
      static char[] Separators = new char[] { ' ', '.', ',', '!', ':', ';', '(', ')', '\t', '\n', '\'', '"', '"' };
  
      public string Text { get; private set; }
      public LineContainer Lines { get; private set; }
  
      public string[] Words { get { return Text.Split(Separators); } }
  
      public Book(string[] lines)
      {
          Text = "";
          for (int i = 0; i &lt; lines.Length; i++)
          {
              Text += lines[i] + Environment.NewLine;
          }
  
          Lines = new LineContainer(lines);
      }
  
  
  class LineContainer
  {
      private List&lt;Line&gt; Lines;
      public int Count { get { return Lines.Count; } }
  
      public LineContainer(string[] lines)
      {
          Lines = new List&lt;Line&gt;();
          for (int i = 0; i &lt; lines.Length; i++)
          {
              Lines.Add(new Line(lines[i], i));
          }
      }
  
      public Line Get(int index)
      {
          return Lines[index];
      }
  }
  
  class Line
  {
      static char[] Separators = new char[] { ' ', '.', ',', '!', ':', ';', '(', ')', '\t', '\n', '\'', '"', '"' };
  
      public string Text { get; private set; }
      public int Length { get { return Text.Length; } }
      public int Number { get; private set; }
  
      public string[] Words { get { return Text.Split(Separators); } }
  
      public Line(string text, int number)
      {
          Text = text;
          Number = number;
      }
  }
  </code></pre>