21
3
In English, there is the fun and simple difference between an
and a
: you use an
when preceding a word starting with a vowel sound, and a
when the word starts with a consonant sound.
For the sake of simplicity in this challenge, an
precedes a word that starts with a vowel (aeiou
), and a
precedes a word that starts with a consonant.
Input
A string comprising only printable ASCII characters, with [?]
appearing in places where you must choose to insert an
or a
. [?]
will always appear before a word. You can assume that the sentence will be grammatically correct and formatted like normal.
Output
The input string with [?]
replaced with the appropriate word (an
or a
). You do have to worry about capitalization!
When to Capitalize
Capitalize a word if it is preceded by no characters (is the first one in the input) or if it is preceded by one of .?!
followed by a space.
Examples
Input: Hello, this is [?] world!
Output: Hello, this is a world!
Input: How about we build [?] big building. It will have [?] orange banana hanging out of [?] window.
Output: How about we build a big building. It will have an orange banana hanging out of a window.
Input: [?] giant en le sky.
Output: A giant en le sky.
Input: [?] yarn ball? [?] big one!
Output: A yarn ball? A big one!
Input: [?] hour ago I met [?] European.
Output: A hour ago I met an European.
Input: Hey sir [Richard], how 'bout [?] cat?
Output: Hey sir [Richard], how 'bout a cat?
This is code-golf, so shortest code in bytes wins!
OK thanks. Can we assume no inputs will have extra spaces between the
[?]
and the word? – James – 2016-09-15T03:20:37.777@DJMcMayhem, you can assume that the sentences will be grammatically correct – Daniel – 2016-09-15T03:21:30.910
@Dopapp I pronounce
– DanTheMan – 2016-09-15T03:25:02.970historian
ashistorian
, but I was taught to say it asan historian
. (Pronounced asan istorian
) It is hotly debated though: http://www.google.com/search?q=%22an+historian"8Does a/an have to be capitalized in the middle of the input when it comes at the beginning of a sentence? ("This is [?] test. [?] test.") If so, what punctuation can a sentence end with? What about sentences in quotation marks or parentheses? Or abbreviations that end in a period ("E.g. [?] input like this")? Capitalization rules have lots of weird special cases, so please be very explicit about what our programs do or don't need to handle. – DLosc – 2016-09-15T04:25:47.363
1Could you please clarify when to capitalize? The first character? – James – 2016-09-15T04:50:49.053
34You should add the test case
[?] hour ago I met [?] European.
just to make everyone cringe. – Martin Ender – 2016-09-15T09:03:00.820@DLosc, I have made an edit to specify when to capitalize. Is it clear now? – Daniel – 2016-09-15T11:08:41.450
>
[?]other example
? 2, I guess we can expect that [?] is not at the end of the text or a sentence.@Titus 1. There won't be anything like that. 2. Correct – Daniel – 2016-09-15T11:17:36.910
@DJMcMayhem, I edited it. Is it clearer when to capitalize? – Daniel – 2016-09-15T11:20:08.653
@MartinEnder You are so evil... that is soooo wrong... – beaker – 2016-09-15T15:32:30.093
1Now we must have
[?] hour ago I met [?] horse.
– beaker – 2016-09-15T15:39:37.450@Dopapp Yes, that's much clearer. Now it just needs a couple test cases with sentence-initial
[?]
after multiple different punctuation characters. – DLosc – 2016-09-15T22:05:44.637@DLosc see the last test case – Daniel – 2016-09-15T23:19:48.670
What about “An honest” or “A unicorn”? English is great. – Stan Strum – 2017-12-06T18:28:33.120