From 3ca49e564a342f88b74553b6241ff88bb3ccf006 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 22 Nov 2021 01:26:55 +0200 Subject: [PATCH] feat: add "Lab4.FirstEqualLast" using strings --- Lab4.FirstEqualLast/Duomenys.txt | 9 +++++ .../Lab4.FirstEqualLast.csproj | 10 +++++ Lab4.FirstEqualLast/Program.cs | 15 ++++++++ Lab4.FirstEqualLast/TaskUtils.cs | 38 +++++++++++++++++++ Lab4.sln | 6 +++ 5 files changed, 78 insertions(+) create mode 100644 Lab4.FirstEqualLast/Duomenys.txt create mode 100644 Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj create mode 100644 Lab4.FirstEqualLast/Program.cs create mode 100644 Lab4.FirstEqualLast/TaskUtils.cs diff --git a/Lab4.FirstEqualLast/Duomenys.txt b/Lab4.FirstEqualLast/Duomenys.txt new file mode 100644 index 0000000..4a915c7 --- /dev/null +++ b/Lab4.FirstEqualLast/Duomenys.txt @@ -0,0 +1,9 @@ +V. M. Putinas +Margi sakalai +Lydėdami gęstančią žarą vėlai +Pakilo į dangų;;, margi sakalai. +Paniekinę žemės vylingus sapnus, +Padangėje ištiesė,,; savo sparnus. +Ir tarė margieji: negrįšim į žemę, +Kol josios kalnai ir pakalnės aptemę. +... diff --git a/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj b/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Lab4.FirstEqualLast/Lab4.FirstEqualLast.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Lab4.FirstEqualLast/Program.cs b/Lab4.FirstEqualLast/Program.cs new file mode 100644 index 0000000..7ae957b --- /dev/null +++ b/Lab4.FirstEqualLast/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Lab4.FirstEqualLast +{ + public class Program + { + public static void Main(string[] args) + { + const string CFd = "Duomenys.txt"; + char [] punctuation = {' ','.',',','!','?',':',';','(',')','\t'}; + Console.WriteLine("Sutampančių žodžių {0, 3:d}", TaskUtils.Process(CFd, punctuation)); + } + } +} diff --git a/Lab4.FirstEqualLast/TaskUtils.cs b/Lab4.FirstEqualLast/TaskUtils.cs new file mode 100644 index 0000000..52c4993 --- /dev/null +++ b/Lab4.FirstEqualLast/TaskUtils.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Lab4.FirstEqualLast +{ + public class TaskUtils + { + /** Reads file and finds the number of words having same the first and + the last letters. + @param fin – name of data file + @param punctuation – punctuation marks to separate words */ + public static int Process(string fin, char[] punctuation) + { + string[] lines = File.ReadAllLines(fin, Encoding.UTF8); + int equal = 0; + foreach (string line in lines) + if (line.Length > 0) + equal += FirstEqualLast(line, punctuation); + return equal; + } + + /** Splits line into words and counts the words having same the first and the + last letters. + @param line – string of data + @param punctuation – punctuation marks to separate words */ + private static int FirstEqualLast (string line, char[] punctuation) + { + string[] parts = line.Split(punctuation, + StringSplitOptions.RemoveEmptyEntries); + int equal = 0; + foreach (string word in parts) + if (word[0] == word[word.Length - 1]) + equal++; + return equal; + } + } +} diff --git a/Lab4.sln b/Lab4.sln index ed0ec99..c41487d 100644 --- a/Lab4.sln +++ b/Lab4.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lab4.RemoveComments", "Lab4 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.LetterFrequency", "Lab4.LetterFrequency\Lab4.LetterFrequency.csproj", "{F1797B5C-1541-4821-9485-7C3520C5DD8D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.FirstEqualLast", "Lab4.FirstEqualLast\Lab4.FirstEqualLast.csproj", "{83CB5DEC-25E5-42FF-8601-9BF8DA73E397}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -31,6 +33,10 @@ Global {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Debug|Any CPU.Build.0 = Debug|Any CPU {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F1797B5C-1541-4821-9485-7C3520C5DD8D}.Release|Any CPU.Build.0 = Release|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83CB5DEC-25E5-42FF-8601-9BF8DA73E397}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE