From be6a3ec986b9a1925a1f30aea834e5cdac4c785e Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Wed, 18 Jul 2018 15:30:46 -0500 Subject: [PATCH 1/3] put project into 2 functions, cleaned up --- PigLatin/.vscode/launch.json | 28 +++++++++ PigLatin/.vscode/tasks.json | 15 +++++ PigLatin/PigLatin.cs | 118 +++++++++++++++++++++++++++++++++-- 3 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 PigLatin/.vscode/launch.json create mode 100644 PigLatin/.vscode/tasks.json diff --git a/PigLatin/.vscode/launch.json b/PigLatin/.vscode/launch.json new file mode 100644 index 00000000..dbee6e56 --- /dev/null +++ b/PigLatin/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/PigLatin.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/PigLatin/.vscode/tasks.json b/PigLatin/.vscode/tasks.json new file mode 100644 index 00000000..a705ed07 --- /dev/null +++ b/PigLatin/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/PigLatin.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/PigLatin/PigLatin.cs b/PigLatin/PigLatin.cs index 702647dd..faabefdd 100644 --- a/PigLatin/PigLatin.cs +++ b/PigLatin/PigLatin.cs @@ -7,15 +7,121 @@ class Program public static void Main() { // your code goes here + // char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',}; + // Console.WriteLine("Enter a sentence"); + // string sentence = Console.ReadLine(); + + // string[] words = sentence.Split(' '); + + // foreach (var wordInArr in words) + // { + // string word = wordInArr; + // Console.WriteLine(word); + + // } + + TranslateWords(); + PlayAgain(); + + void TranslateWords() { + Console.WriteLine("Please type in a word or sentence and press the Enter key."); + string sentence = Console.ReadLine(); + string[] words = sentence.Split(' '); + string[] translatedWords = new string[words.Length]; + for (int i = 0; i < words.Length; i++) + { + char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'y', 'Y'}; + string word = words[i]; + int wordLength = word.Length; + int firstVowelIndex = word.IndexOfAny(vowels); + string firstHalf = word.Substring(0, firstVowelIndex); + int secondHalfLength = wordLength - firstVowelIndex; + string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); + if (firstVowelIndex == 0) + { + string composite = secondHalf + firstHalf + "yay"; + translatedWords[i] = composite; + } + else + { + string composite = secondHalf + firstHalf + "ay"; + translatedWords[i] = composite; + } + } + string translatedSentence = String.Join(" ", translatedWords); + Console.WriteLine(translatedSentence); + } + + void PlayAgain() { + + string[] yes = { Convert.ToString('y'), Convert.ToString('Y'), "yes", "Yes", "YES"}; + Console.WriteLine("Do you want to try again? [y/n]"); + for (int i = 0; i < yes.Length; i++) + { + string choice = Console.ReadLine(); + if (choice == yes[i]) + { + TranslateWords(); + PlayAgain(); + } + else + { + Console.WriteLine("Bye!"); + Environment.Exit(0); + } + + } + + + } + + + + + // TranslateWord(); + + // void TranslateWord(){ + + // char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',}; + // char[] vowelsy = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'y', 'Y'}; + + // Console.WriteLine("Enter a word"); + // string word = Console.ReadLine(); + // int wordLength = word.Length; + // string ay = "ay"; + // string yay = "yay"; + // Console.WriteLine("The wordLength of the word \"" + word + "\" is " + wordLength + " characters long."); + + // int firstVowelIndex = word.IndexOfAny(vowels); + // Console.WriteLine("The index of the first vowel in the word \"" + word + "\" is " + firstVowelIndex); + + // string firstHalf = word.Substring(0, firstVowelIndex); + // Console.WriteLine("The firstHalf is " + firstHalf); + + // int secondHalfLength = wordLength - firstVowelIndex; + // Console.WriteLine("The length of the second half is " + secondHalfLength); + + // string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); + // Console.WriteLine("The secondHalf is " + secondHalf); + + // if (firstVowelIndex == 0) + // { + // string composite = secondHalf + firstHalf + yay; + // Console.WriteLine("Your pig latin translation of \"" + word + "\" is " + composite); + // } + + // else + // { + // string composite = secondHalf + firstHalf + ay; + // Console.WriteLine("Your pig latin translation of \"" + word + "\" is \"" + composite + "\"."); + // } + + // } + // leave this command at the end so your program does not close automatically - Console.ReadLine(); + } - public static string TranslateWord(string word) - { - // your code goes here - return word; - } } } From 06c4b5ee18477f87714cd7610c22974813c8e737 Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Wed, 18 Jul 2018 19:23:39 -0500 Subject: [PATCH 2/3] update to assignment --- PigLatin/PigLatin.cs | 67 -------------------------------------------- 1 file changed, 67 deletions(-) diff --git a/PigLatin/PigLatin.cs b/PigLatin/PigLatin.cs index faabefdd..3f408763 100644 --- a/PigLatin/PigLatin.cs +++ b/PigLatin/PigLatin.cs @@ -6,21 +6,6 @@ class Program { public static void Main() { - // your code goes here - // char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',}; - - // Console.WriteLine("Enter a sentence"); - // string sentence = Console.ReadLine(); - - // string[] words = sentence.Split(' '); - - // foreach (var wordInArr in words) - // { - // string word = wordInArr; - // Console.WriteLine(word); - - // } - TranslateWords(); PlayAgain(); @@ -52,9 +37,7 @@ void TranslateWords() { string translatedSentence = String.Join(" ", translatedWords); Console.WriteLine(translatedSentence); } - void PlayAgain() { - string[] yes = { Convert.ToString('y'), Convert.ToString('Y'), "yes", "Yes", "YES"}; Console.WriteLine("Do you want to try again? [y/n]"); for (int i = 0; i < yes.Length; i++) @@ -70,58 +53,8 @@ void PlayAgain() { Console.WriteLine("Bye!"); Environment.Exit(0); } - } - - } - - - - - // TranslateWord(); - - // void TranslateWord(){ - - // char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U',}; - // char[] vowelsy = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'y', 'Y'}; - - // Console.WriteLine("Enter a word"); - // string word = Console.ReadLine(); - // int wordLength = word.Length; - // string ay = "ay"; - // string yay = "yay"; - // Console.WriteLine("The wordLength of the word \"" + word + "\" is " + wordLength + " characters long."); - - // int firstVowelIndex = word.IndexOfAny(vowels); - // Console.WriteLine("The index of the first vowel in the word \"" + word + "\" is " + firstVowelIndex); - - // string firstHalf = word.Substring(0, firstVowelIndex); - // Console.WriteLine("The firstHalf is " + firstHalf); - - // int secondHalfLength = wordLength - firstVowelIndex; - // Console.WriteLine("The length of the second half is " + secondHalfLength); - - // string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); - // Console.WriteLine("The secondHalf is " + secondHalf); - - // if (firstVowelIndex == 0) - // { - // string composite = secondHalf + firstHalf + yay; - // Console.WriteLine("Your pig latin translation of \"" + word + "\" is " + composite); - // } - - // else - // { - // string composite = secondHalf + firstHalf + ay; - // Console.WriteLine("Your pig latin translation of \"" + word + "\" is \"" + composite + "\"."); - // } - - // } - - // leave this command at the end so your program does not close automatically - } - } } From b07a926631f24a939dc2eeb624253867ebe9a381 Mon Sep 17 00:00:00 2001 From: "James.Woodard" Date: Wed, 25 Jul 2018 14:51:53 -0500 Subject: [PATCH 3/3] added ungraceful exception handling for words with no syllables --- PigLatin/PigLatin.cs | 61 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/PigLatin/PigLatin.cs b/PigLatin/PigLatin.cs index 3f408763..0230dd75 100644 --- a/PigLatin/PigLatin.cs +++ b/PigLatin/PigLatin.cs @@ -11,32 +11,63 @@ public static void Main() void TranslateWords() { Console.WriteLine("Please type in a word or sentence and press the Enter key."); - string sentence = Console.ReadLine(); + string sentence = Console.ReadLine().ToLower(); string[] words = sentence.Split(' '); string[] translatedWords = new string[words.Length]; - for (int i = 0; i < words.Length; i++) + for (int j = 0; j < words.Length; j++) { - char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', 'y', 'Y'}; - string word = words[i]; - int wordLength = word.Length; - int firstVowelIndex = word.IndexOfAny(vowels); - string firstHalf = word.Substring(0, firstVowelIndex); - int secondHalfLength = wordLength - firstVowelIndex; - string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); - if (firstVowelIndex == 0) + char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y',}; + string a = "a"; + string e = "e"; + string i = "i"; + string o = "o"; + string u = "u"; + string y = "y"; + string word = words[j]; + int wordLength = word.Length; + + if (word.Contains(a) || word.Contains(e) || word.Contains(i) || word.Contains(o) || word.Contains(u) || word.Contains(y)) { - string composite = secondHalf + firstHalf + "yay"; - translatedWords[i] = composite; + int firstVowelIndex = word.IndexOfAny(vowels); + string firstHalf = word.Substring(0, firstVowelIndex); + int secondHalfLength = wordLength - firstVowelIndex; + string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); + if (firstVowelIndex == 0) + { + string composite = secondHalf + firstHalf + "yay"; + translatedWords[j] = composite; + } + else + { + string composite = secondHalf + firstHalf + "ay"; + translatedWords[j] = composite; + } } else { - string composite = secondHalf + firstHalf + "ay"; - translatedWords[i] = composite; + translatedWords[j] = word + "yay"; } + + + // int firstVowelIndex = word.IndexOfAny(vowels); + // string firstHalf = word.Substring(0, firstVowelIndex); + // int secondHalfLength = wordLength - firstVowelIndex; + // string secondHalf = word.Substring(firstVowelIndex, secondHalfLength); + // if (firstVowelIndex == 0) + // { + // string composite = secondHalf + firstHalf + "yay"; + // translatedWords[j] = composite; + // } + // else + // { + // string composite = secondHalf + firstHalf + "ay"; + // translatedWords[j] = composite; + // } } string translatedSentence = String.Join(" ", translatedWords); Console.WriteLine(translatedSentence); } + void PlayAgain() { string[] yes = { Convert.ToString('y'), Convert.ToString('Y'), "yes", "Yes", "YES"}; Console.WriteLine("Do you want to try again? [y/n]"); @@ -55,6 +86,8 @@ void PlayAgain() { } } } + + } } }