diff --git a/Lab4.ChainedWords/Knyga.txt b/Lab4.ChainedWords/Knyga.txt
new file mode 100644
index 0000000..e1b448d
--- /dev/null
+++ b/Lab4.ChainedWords/Knyga.txt
@@ -0,0 +1,7 @@
+Vienas QRSs!!?SABC V ABC
+.
+CEF FEG GOF
+
+QRS SABC
+
+
diff --git a/Lab4.ChainedWords/Lab4.ChainedWords.csproj b/Lab4.ChainedWords/Lab4.ChainedWords.csproj
new file mode 100644
index 0000000..23df604
--- /dev/null
+++ b/Lab4.ChainedWords/Lab4.ChainedWords.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ netcoreapp2.1
+
+
+
diff --git a/Lab4.ChainedWords/Program.cs b/Lab4.ChainedWords/Program.cs
new file mode 100644
index 0000000..a66aa2d
--- /dev/null
+++ b/Lab4.ChainedWords/Program.cs
@@ -0,0 +1,18 @@
+using System;
+
+namespace Lab4.ChainedWords
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ string input = "Knyga.txt";
+ string output1 = "Rodikliai.txt";
+ string output2 = "ManoKnyga.txt";
+ string punctuation = " ,.;!?";
+ TaskUtils.ProcessChains(input, output1, punctuation);
+ TaskUtils.ProcessAligned(input, output2, punctuation);
+ }
+ }
+}
+
diff --git a/Lab4.ChainedWords/TaskUtils.cs b/Lab4.ChainedWords/TaskUtils.cs
new file mode 100644
index 0000000..806765d
--- /dev/null
+++ b/Lab4.ChainedWords/TaskUtils.cs
@@ -0,0 +1,122 @@
+using System;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace Lab4.ChainedWords
+{
+ static class TaskUtils
+ {
+ private static MatchCollection MatchByWords(string text, string punctuation)
+ {
+ string pattern = string.Format(@"[^{0}\n]+", Regex.Escape(punctuation));
+ return Regex.Matches(text, pattern, RegexOptions.IgnoreCase);
+ }
+
+ private static MatchCollection MatchByIntegers(string text)
+ {
+ return Regex.Matches(text, @"\d+");
+ }
+
+ public static List> FindChains(string text, string punctuation)
+ {
+ List> chains = new List>();
+
+ bool startedChain = false;
+ int chainStart = 0;
+
+ MatchCollection matches = MatchByWords(text, punctuation);
+ for (int i = 1; i < matches.Count; i++)
+ {
+ Match prevMatch = matches[i-1];
+ Match match = matches[i];
+ bool matchesCriteria = prevMatch.Value.ToLowerInvariant().Last() == match.Value.ToLowerInvariant().First();
+
+ if (matchesCriteria && !startedChain)
+ {
+ startedChain = true;
+ chainStart = prevMatch.Index;
+ }
+ else if (!matchesCriteria && startedChain)
+ {
+ startedChain = false;
+ int line = text.Substring(0, chainStart).Count(c => c == '\n')+1;
+ string chain = text.Substring(chainStart, match.Index-chainStart).TrimEnd('\n');
+ chains.Add(new Tuple(line, chain));
+ }
+ }
+
+ // Ensure that last chain gets added to list
+ if (startedChain)
+ {
+ int line = text.Substring(0, chainStart).Count(c => c == '\n')+1;
+ string chain = text.Substring(chainStart).TrimEnd('\n');
+ chains.Add(new Tuple(line, chain));
+ }
+
+ return chains;
+ }
+
+ public static List FindIntegers(string text)
+ {
+ List integers = new List();
+
+ MatchCollection matches = MatchByIntegers(text);
+ foreach (Match match in matches)
+ {
+ integers.Add(int.Parse(match.Value));
+ }
+
+ return integers;
+ }
+
+ public static Tuple FindLongestChain(string text, string punctuation)
+ {
+ List> chains = FindChains(text, punctuation);
+
+ if (chains.Count == 0)
+ {
+ return null;
+ }
+
+ Tuple longestChain = chains[0];
+ foreach (var pair in chains)
+ {
+ if (pair.Item2.Length > longestChain.Item2.Length)
+ {
+ longestChain = pair;
+ }
+ }
+
+ return longestChain;
+ }
+
+ public static void ProcessChains(string inputFile, string outputFile, string punctuation)
+ {
+ string text = File.ReadAllText(inputFile, Encoding.UTF8);
+ Tuple longestChain = FindLongestChain(text, punctuation);
+ List integers = FindIntegers(text);
+
+ using (StreamWriter writer = new StreamWriter(outputFile))
+ {
+ if (longestChain != null)
+ {
+ writer.WriteLine("Ilgiausia žodžių grandinė {0} eilutėje: {1}", longestChain.Item1, longestChain.Item2);
+ }
+ else
+ {
+ writer.WriteLine("Nėra žodžių grandinių.");
+ }
+
+ writer.WriteLine("Yra {0} žodžiai sudaryti iš skaitmenų. Jų suma: {1}", integers.Count, integers.Sum());
+ }
+ }
+
+ public static void ProcessAligned(string input, string ouput, string punctuation)
+ {
+ }
+ }
+}
+