-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
120 lines (103 loc) · 3.79 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Linq;
namespace CapstonePigLatin2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Pig Latin Translator!\nPlease enter a word to translate.");
bool translateAgain = true;
while (translateAgain == true)
{
string userWord = Console.ReadLine().ToLower().Trim();
char[] inputArray = userWord.ToCharArray();
int firstVowelIndex = FirstVowel(inputArray);
string pigLatin = "";
bool charValid = true;
foreach (var letter in userWord)
{
if (char.IsDigit(letter) || char.IsPunctuation(letter) || char.IsSymbol(letter) || char.IsWhiteSpace(letter))
{
charValid = false;
Console.WriteLine("Error.\nMust contain one word with no numbers, special characters, or punctuation");
Continue();
}
}
if (charValid && firstVowelIndex == 0)
{
pigLatin = userWord + "way";
Console.WriteLine($"Translation: {pigLatin}");
}
else if (charValid && firstVowelIndex == -1)
{
pigLatin = userWord;
if (pigLatin == "")
{
Console.WriteLine("Must enter a word.");
}
else
{
Console.WriteLine("Must contain vowel to properly translate.");
Console.WriteLine($"Translation: {pigLatin}");
}
}
else if (charValid)
{
string pretranslated = userWord.Substring(firstVowelIndex);
string posttranslated = userWord.Substring(0, firstVowelIndex) + "ay";
pigLatin = pretranslated + posttranslated;
Console.WriteLine($"Translation: {pigLatin}");
}
translateAgain = Continue();
}
}
public static bool Continue()
{
bool playAgain;
Console.WriteLine("Enter a new word? (y/n): ");
string answer = Console.ReadLine();
if (answer == "y" || answer == "Y")
{
playAgain = true;
Console.WriteLine("Enter a word below.");
}
else if (answer == "n" || answer == "N")
{
playAgain = false;
Console.WriteLine("Thank you for using the Pig Latin Translator.");
}
else
{
Console.WriteLine("Input not vaild. Please try again.");
playAgain = Continue();
}
return playAgain;
}
public static bool IsVowel(char v)
{
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
foreach (char vowel in vowels)
{
if (vowel == v)
{
return true;
}
}
return false;
}
public static int FirstVowel(char[] userInput)
{
for (int i = 0; i < userInput.Length; i++)
{
char letter = userInput[i];
if (IsVowel(letter))
{
return i;
}
}
Console.WriteLine("Error. Please try again");
return -1;
}
}
}