Let's assume that any word that begins with a vowel will be preceded by "an" and that all other words will be preceded by "a".
string getArticle(string forWord)
{
var vowels = new List<char> { 'A', 'E', 'I', 'O', 'U' };
var firstLetter = forWord[0];
var firstLetterCapitalized = char.ToUpper(firstLetter);
var beginsWithVowel = vowels.Contains(firstLetterCapitalized);
if (beginsWithVowel)
return "an";
return "a";
}
Could this be simplified and improved? Of course. However, it should serve as somewhere from which to start.
Less readable but shorter versions exists, such as:
string getArticle(string forWord) => (new List<char> { 'A', 'E', 'I', 'O', 'U' }).Contains(char.ToUpper(forWord[0])) ? "an" : "a";
However, both of these ignore edge cases such as forWord
being null or empty.