Alright, this is a tricky one. I'm looking to make a function which takes an input string, and returns it with a grammatically correct appendix.
Examples (input -> desired output):
- apple -> an apple
- car -> a car
- honour -> an honour
At first I empty-headedly wrote a simple-enough function to check for a vowel at the start of the word and return one of the appendixes based on that... but then I was reminded that this isn't the only time to return "an" instead of "a", and that really it's based on the sound of the start of the input word. So my question is; what would be a good approach to translating this into code, without having to hardcode in a bunch of "special-case" words (or an entire dictionary!)?
I'm working in C# for this. I would prefer to write the code rather than use an external library, provided it wouldn't take up a large amount of time. The less dependencies the better!
EDIT 1
The initial code:
public static string ChooseAppendix(string inputString, bool includeInputString = true, bool capitalizeAppendix = false)
{
if (string.IsNullOrEmpty(inputString))
throw new Exception("Cannot choose an appendix for an empty string!");
char c = inputString.ToLower()[0];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
if (includeInputString)
return capitalizeAppendix ? $"An {inputString}" : $"an {inputString}";
else
return capitalizeAppendix ? "An" : "an";
}
else
{
if (includeInputString)
return capitalizeAppendix ? $"A {inputString}" : $"a {inputString}";
else
return capitalizeAppendix ? "A" : "a";
}
}