diff --git a/WorkingWithText/Program.cs b/WorkingWithText/Program.cs index 733cddd..b869520 100644 --- a/WorkingWithText/Program.cs +++ b/WorkingWithText/Program.cs @@ -11,9 +11,63 @@ public static class WorkingWithText // to see if there are duplicates. If so, return bool True; otherwise, return bool False. public static bool AreThereDuplicates(string hyphenNum) { - return default; + bool result = false; + try + { + List nums = hyphenNum.Split('-').Select(int.Parse).ToList(); + + for (int i = 0; i < hyphenNum.Count(); i++) + { + for (int j = 0; j < nums.Count(); j++) + { + try + { + if (i != j) + { + if (Convert.ToInt32(nums[i]) == Convert.ToInt32(nums[j])) + { + result = true; + return result; + } + } + } + catch + { + } + } + } + } + catch + { + return result; + } + + //char[] hyphenCheck = hyphenNum.ToCharArray(); + //result = Char.IsNumber(hyphenCheck[0]); + //result = Char.IsNumber(hyphenCheck[hyphenCheck.Length - 1]); + //for (int i = 0; i < hyphenCheck.Length; i++) + //{ + // try + // { + // if (Convert.ToString(hyphenCheck[i]) != "-" || Char.IsNumber(hyphenCheck[0]) == false) + // { + // result = false; + // break; + // } + // else if (hyphenCheck[i] == hyphenCheck[i + 1]) + // { + // result = false; + // break; + // } + // } + // catch + // { + + // } + //} + return result; } - + // 2- Write a method that accepts a string of numbers separated by a hyphen. If the input // is NOT in the correct format OR is NOT consecutive then return bool False. If the format // is correct AND the numbers are consecutive, return bool True. For @@ -21,7 +75,27 @@ public static bool AreThereDuplicates(string hyphenNum) // Do not use .Sort, it will cause the test to pass when it actually does not. public static bool IsConsecutive(string hyphenNum) { - return default; + bool isConsecutiveAcending = false; + bool isConsecutiveDecending = false; + + try + { + List nums = hyphenNum.Split('-').Select(int.Parse).ToList(); + isConsecutiveAcending = !nums.Select((i, j) => i - j).Distinct().Skip(1).Any(); + isConsecutiveDecending = !nums.Select((i, j) => i + j).Distinct().Skip(1).Any(); + } + catch + { + return false; + } + if (isConsecutiveAcending == true || isConsecutiveDecending == true) + { + return true; + } + else + { + return false; + } } // 3- Write a method that accepts a string of a time 24-hour time format @@ -30,7 +104,21 @@ public static bool IsConsecutive(string hyphenNum) // consider it as False. Make sure that its returns false if any letters are passed. public static bool IsValidTime(string hyphenNum) { - return default; + bool result = false; + DateTime dt; + try + { + dt = Convert.ToDateTime(hyphenNum); + if(hyphenNum == dt.ToString("HH:mm")) + { + result = true; + } + + } + catch + { + } + return result; } // 4- Write a method that accepts a string of a few words separated by a space. Use the @@ -40,15 +128,51 @@ public static bool IsValidTime(string hyphenNum) // Trim off unneeded spaces. public static string PascalConverter(string aFewWords) { - return default; + if(aFewWords == "" || aFewWords == null) + { + return aFewWords; + } + string lowerCase = aFewWords.ToLower(); + string[] separated = aFewWords.Split(' '); + string temp = ""; + string result = ""; + for (int i = 0; i < separated.Count(); i++) + { + temp = separated[i]; + if (temp.Length > 1) + { + result += char.ToUpper(temp[0]) + temp.Substring(1).ToLower(); + } + else + { + result += temp.ToUpper(); + } + } + return result; } // 5- Write a method that accepts an English word. Count the number of vowels // (a, e, i, o, u) in the word. So, if the user enters "inadequate", the program should // return 6. - public static int VowelCounter(string aWord) + public static int VowelCounter(string aWord) { - return default; + char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' }; + aWord = aWord.ToLower(); + char[] characters = aWord.ToCharArray(); + int vowelCount = 0; + aWord = aWord.ToLower(); + + for(int i = 0; i