From 939188ae5e4debc19da7e6ed325b6e86243a23db Mon Sep 17 00:00:00 2001 From: Michael Rich Date: Mon, 15 Oct 2018 18:36:25 -0500 Subject: [PATCH 1/2] Program translates most words to pig latin but words that dont contain a normal vowel break the code ex. my, tsktsk, cry, fly. Also need to add comments --- PigLatin/PigLatin.cs | 49 +++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/PigLatin/PigLatin.cs b/PigLatin/PigLatin.cs index 702647dd..30042536 100644 --- a/PigLatin/PigLatin.cs +++ b/PigLatin/PigLatin.cs @@ -1,21 +1,46 @@ using System; +using System.Collections.Generic; namespace PigLatin { class Program { - public static void Main() - { - // your code goes here - - // leave this command at the end so your program does not close automatically - Console.ReadLine(); + public static void Main(string[] args) + { + Console.WriteLine("Enter a sentence to convert to PigLatin:"); + string sentence = Console.ReadLine(); + string pigLatin = ToPigLatin(sentence); + Console.WriteLine(pigLatin.ToLower()); } - - public static string TranslateWord(string word) - { - // your code goes here - return word; + + static string ToPigLatin (string sentence) + { + List latin = new List(); + + //splits user entered input + foreach (string word in sentence.Split(' ')) + { + int firstVowelIndex = word.IndexOfAny(new char[] {'A','E','I','O','U','a','e','i','o','u',}); + int firstYIndex = word.IndexOfAny(new char[] {'Y','y'}); + string first = word.Substring(0, firstVowelIndex); + string rest = word.Substring(firstVowelIndex); + + if ((firstYIndex > 0) && (firstYIndex < firstVowelIndex) && (word.ToLower().Contains("y"))) + { + string firstY = word.Substring(0, firstYIndex); + string restY = word.Substring(firstYIndex); + latin.Add(restY + firstY + "ay"); + } + else if (firstVowelIndex > 0) + { + latin.Add(rest + first + "ay"); + } + else + { + latin.Add(word + "yay"); + } + } + return string.Join(" ", latin); } } -} +} \ No newline at end of file From 00d85df72c0fd4b16a53bdb5953c7d0578dd2362 Mon Sep 17 00:00:00 2001 From: Michael Rich Date: Mon, 15 Oct 2018 22:55:41 -0500 Subject: [PATCH 2/2] added ability to translate words without any normal vowels --- PigLatin/PigLatin.cs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/PigLatin/PigLatin.cs b/PigLatin/PigLatin.cs index 30042536..6ceca99b 100644 --- a/PigLatin/PigLatin.cs +++ b/PigLatin/PigLatin.cs @@ -6,40 +6,56 @@ namespace PigLatin class Program { public static void Main(string[] args) - { + { + //stores user input into string sentence Console.WriteLine("Enter a sentence to convert to PigLatin:"); string sentence = Console.ReadLine(); + //takes translated sentence from ToPigLatin and then writes it into console string pigLatin = ToPigLatin(sentence); Console.WriteLine(pigLatin.ToLower()); } + //translates sentence static string ToPigLatin (string sentence) - { + { + //creates list of words that the translated words are added to List latin = new List(); //splits user entered input foreach (string word in sentence.Split(' ')) { + //checks position each vowel and y int firstVowelIndex = word.IndexOfAny(new char[] {'A','E','I','O','U','a','e','i','o','u',}); int firstYIndex = word.IndexOfAny(new char[] {'Y','y'}); - string first = word.Substring(0, firstVowelIndex); - string rest = word.Substring(firstVowelIndex); - + + //checks if Y acts as vowel if ((firstYIndex > 0) && (firstYIndex < firstVowelIndex) && (word.ToLower().Contains("y"))) { string firstY = word.Substring(0, firstYIndex); string restY = word.Substring(firstYIndex); latin.Add(restY + firstY + "ay"); } + //checks if word contains normal vowel else if (firstVowelIndex > 0) { - latin.Add(rest + first + "ay"); + string first = word.Substring(0, firstVowelIndex); + string rest = word.Substring(firstVowelIndex); + latin.Add(rest + first + "ay"); + } + //checks if y acts as vowel in word with no other vowels + else if (firstYIndex > 0) + { + string firstY = word.Substring(0, firstYIndex); + string restY = word.Substring(firstYIndex); + latin.Add(restY + firstY + "ay"); } + //checks if word starts with vowel or contains only consonents else { latin.Add(word + "yay"); } - } + } + //joins translated words together return string.Join(" ", latin); } }