using System.Text; using System.Text.Json; namespace MethodLibrary { public class Strings : IMethodCollection { public void DisplayAllMethods() { Console.WriteLine($"AddSeparator(\"ABCD\", \"^\"): {AddSeparator("ABCD", "^")}"); Console.WriteLine($"AddSeparator(\"chocolate\", \"-\"): {AddSeparator("chocolate", "-")}"); Console.WriteLine($"IsPalindrome(\"eye\"): {IsPalindrome("eye")}"); Console.WriteLine($"IsPalindrome(\"home\"): {IsPalindrome("home")}"); Console.WriteLine($"LengthOfAString(\"computer\"): {LengthOfAString("computer")}"); Console.WriteLine($"LengthOfAString(\"ice cream\"): {LengthOfAString("ice cream")}"); Console.WriteLine($"StringInReverseOrder(\"qwerty\"): {StringInReverseOrder("qwerty")}"); Console.WriteLine($"StringInReverseOrder(\"oe93 kr\"): {StringInReverseOrder("oe93 kr")}"); Console.WriteLine($"NumberOfWords(\"This is sample sentence\"): {NumberOfWords("This is sample sentence")}"); Console.WriteLine($"NumberOfWords(\"OK\"): {NumberOfWords("OK")}"); Console.WriteLine($"RevertWordsOrder(\"John Doe.\"): {RevertWordsOrder("John Doe.")}"); Console.WriteLine($"RevertWordsOrder(\"A, B. C\"): {RevertWordsOrder("A, B. C")}"); Console.WriteLine($"HowManyOccurrences(\"do it now\", \"do\"): {HowManyOccurrences("do it now", "do")}"); Console.WriteLine($"HowManyOccurrences(\"empty\", \"d\"): {HowManyOccurrences("empty", "d")}"); Console.WriteLine($"SortCharactersDescending(\"onomatopoeia\"): {JsonSerializer.Serialize(SortCharactersDescending("onomatopoeia"))}"); Console.WriteLine($"SortCharactersDescending(\"fohjwf42os\"): {JsonSerializer.Serialize(SortCharactersDescending("fohjwf42os"))}"); Console.WriteLine($"CompressString(\"kkkktttrrrrrrrrrr\"): {CompressString("kkkktttrrrrrrrrrr")}"); Console.WriteLine($"CompressString(\"p555ppp7www\"): {CompressString("p555ppp7www")}"); } public string AddSeparator(string str, string seperator) { return string.Join(seperator, str.ToList()); } public bool IsPalindrome(string str) { bool isPalindrome = true; var stringParts = str.ToList(); for (int i = 0; i < stringParts.Count - 1; i++) { if (!stringParts[i].Equals(stringParts[stringParts.Count - i - 1])) isPalindrome = false; } return isPalindrome; } public int LengthOfAString(string str) { return str.Count(); } public string StringInReverseOrder(string str) { return string.Join("", str.Reverse()); } public int NumberOfWords(string str) { return str.Split(" ").Count(); } public string RevertWordsOrder(string str) { return string.Join(" ", str.Split(" ").Reverse()); } public int HowManyOccurrences(string str, string strToCheck) { return str.Split(" ").Count(x => x.Equals(strToCheck)); } public char[] SortCharactersDescending(string str) { return str.OrderBy(x => (char)x).Reverse().ToArray(); } public string CompressString(string str) { StringBuilder retval = new StringBuilder(); int count = 1; for (int i = 1; i < str.Length; i++) { if (str[i] == str[i - 1]) { count++; } else { retval.Append(str[i - 1]); retval.Append(count); count = 1; } } retval.Append(str[^1]); retval.Append(count); return retval.ToString(); } } }