using System.IO; using System.Text; // ``` // one two three four five // six seven eight nine ten // ``` // // ``` // three apple orange cherry eight orchid sunflower // ``` // // ``` // one two three apple orange cherry eight six seven eight nine ten orchid sunflower // * * * // match match end of file 1 // ``` var result = MergeAllText( fin: """ one two three four five six seven eight nine ten """, // """ // Saulė patekėjo anksti, o dangus buvo be debesų. Paukščiai čiulbėjo garsiai; oras buvo šiltas ir gaivus. // Ant kalno stovėjo sena pilis, kurios langai spindėjo ryto šviesoje. Žmonės miestelyje ruošėsi šventei – visi buvo gerai nusiteikę. // Biblioteka atsidarė anksti, ir studentai skubėjo į paskaitas. Profesorius pasakojo apie matematiką ir istoriją. // Keliautojas aplankė muziejų, kur eksponatai buvo įdomūs. Architektas projektavo naują pastatą miesto centre. // """, fin2: """ three apple orange cherry eight """, // """ // Ryte dangus buvo mėlynas, o saulė švietė virš kalno. Pilis atrodė išdidžiai ir tyliai stebėjo miestelį. // Vėjas pūtė švelniai; medžių lapai šnarėjo tarsi jūra. Visi laukė šventės pradžios – oras buvo kupinas džiaugsmo. // Biblioteka buvo pilna studentų, kurie mokėsi matematiką. Profesorius dėstė apie architektūrą ir istoriją mieste. // Keliautojas fotografavo eksponatai muziejuje buvo įspūdingi. Architektas pristatė projektą komisijai vakar vakare. // """, punctuation: [] ); Console.WriteLine(result); static string MergeAllText(string fin, string fin2, char[] punctuation) { int i = 0, j = 0; string line, line2; bool readFromFirst = true; StringBuilder sb = new(); using StringReader reader = new(fin); using StringReader reader2 = new(fin2); string? NextFileOneLine() { var content = reader.ReadLine(); i = 0; return content; } string? NextFileTwoLine() { var content = reader2.ReadLine(); j = 0; return content; } // advance a line from file 1 if it is available // if it was, then also advance a line from file 2 if it is available while ((line = NextFileOneLine()) != null && (line2 = NextFileTwoLine()) != null) { string[] words = line.ToLower().Split(punctuation, StringSplitOptions.RemoveEmptyEntries); string[] words2 = line2.ToLower().Split(punctuation, StringSplitOptions.RemoveEmptyEntries); // if (!readFromFirst) // { // continue; // } Console.WriteLine("outer: i:{0}, j:{1}", i, j); // for this line, we'll iterate until we run out of words on the line while (i < words.Length - 1 && j < words2.Length - 1) { Console.WriteLine("iterate line: i:{0}/{1}, j:{2}/{3}", i, words.Length, j, words2.Length); if (readFromFirst) { while (i < words.Length) { // copy the words pointed to by i until EOF 1 (j bounds check) // or until the next-up file 2 word matches the new file 1 word. // Then we: // - exit this inner loop // - mark that on the next pass for this line we should read from file 2 sb.Append(" "); sb.Append(words[i]); Console.WriteLine("DEBUG[1]: i:{0} {1}, j:{2} {3}", i, words[i], j, words2[j]); if (j < words.Length && words[i] == words2[j]) { i++; readFromFirst = false; break; } i++; } } else { while (j < words2.Length) { sb.Append(" "); sb.Append(words2[j]); Console.WriteLine("DEBUG[2]: i:{0} {1}, j:{2} {3}", i, words[i], j, words2[j]); if (i < words.Length && words2[j] == words[i]) { j++; readFromFirst = true; break; } j++; } } // wait, do we never reset i or j after processing one line? } } return sb.ToString(); }