From 43fe49a5be339adab15576dbae74608e7b44451b Mon Sep 17 00:00:00 2001 From: Divlo Date: Fri, 10 Sep 2021 22:18:41 +0200 Subject: [PATCH] feat(solutions): add `acronyms/cs/with-loops` --- .../solutions/cs/with-loops/README.md | 3 + .../solutions/cs/with-loops/Solution.cs | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenges/acronyms/solutions/cs/with-loops/README.md create mode 100644 challenges/acronyms/solutions/cs/with-loops/Solution.cs diff --git a/challenges/acronyms/solutions/cs/with-loops/README.md b/challenges/acronyms/solutions/cs/with-loops/README.md new file mode 100644 index 0000000..7382936 --- /dev/null +++ b/challenges/acronyms/solutions/cs/with-loops/README.md @@ -0,0 +1,3 @@ +# acronyms/cs/with-loops + +Created by [@Divlo](https://github.com/Divlo) on 10 September 2021. diff --git a/challenges/acronyms/solutions/cs/with-loops/Solution.cs b/challenges/acronyms/solutions/cs/with-loops/Solution.cs new file mode 100644 index 0000000..3b69342 --- /dev/null +++ b/challenges/acronyms/solutions/cs/with-loops/Solution.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; + +namespace Solution +{ + class Program + { + static void Main() + { + string line = Console.ReadLine(); + Console.WriteLine(GetAcronym(line)); + } + + /// + /// Returns the acronym of the given string. + /// + /// The string to get the acronym of. + /// The acronym of the given string. + public static string GetAcronym(string line) + { + line = ReplaceString(line, "\"", ""); + string[] words = SplitString(line, " "); + string acronym = ""; + foreach (string word in words) + { + acronym += word[0].ToString().ToUpper(); + } + return acronym; + } + + /// + /// Splits a string into words. + /// + /// The string to split. + /// The delimiter to split the string by. + /// An array of words. + public static string[] SplitString(string source, string delimiter) + { + List result = new List(); + string current = ""; + for (int index = 0; index < source.Length; index++) + { + string character = source[index].ToString(); + if (character == delimiter) + { + result.Add(current); + current = ""; + } + else + { + current += character; + } + } + result.Add(current); + return result.ToArray(); + } + + /// + /// Replaces all instances of a string with another string. + /// + /// The source string. + /// The string to replace. + /// The string to replace with. + /// The new string. + public static string ReplaceString(string source, string oldValue, string newValue) + { + string result = ""; + for (int index = 0; index < source.Length; index++) + { + if (source[index].ToString() == oldValue) + { + result += newValue; + } + else + { + result += source[index]; + } + } + return result; + } + } +}