Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Swep tha vowels!

Note: the title was misspelled intentionally.

Given a string s, swap the first vowel runs of every 2 words. For this challenge, y is considered a vowel.

For example, given an input of "great day sir":

1. Input: "great day sir"
2. Identify pairs of words: "[great day] [sir]" (No word for sir to pair with)
3. Identify the first vowel runs in each word: "[gr[ea]t d[ay]] [s[i]r]"
4. Swap the vowel runs in each pair: "[gr[ay]t d[ea]] [s[i]r]"
5. Return/print: "grayt dea sir"

When there are vowel runs of different lengths, you still swap the whole runs. When a word has more than one vowel runs, you still only swap the first one. When the first or second word of a pair of words does not have a vowel, then you do not swap the vowels for those words.

You may assume that the input only consists of one case of alphabetic letters and the literal space or another constant delimiter.

Standard methods of I/O, standard loopholes apply. Leading/trailing whatevers are okay.

Test cases:

Input -> Output
"great day sir" -> "grayt dea sir"
"ppcg is the best" -> "ppcg is the best" (When there is no vowel to swap, don't swap vowels."
"this is a test case" -> "this is e tast case"
"loooooooooooooong word" -> "long woooooooooooooord"
"great night" -> "grit neaght"
"anything goes" -> "oenything gas"
"qwrtpsdfghjklzxcvbnm aaaaaaaa hi there" -> "qwrtpsdfghjklzxcvbnm aaaaaaaa he thire"
"this is a long test case in case you could not tell" -> "this is o lang tast cese an cise ou cyould net toll"

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ Just a couple of bytes to save by dropping a ? and storing ([aeiouy]+) in a variable: Try it online! \$\endgroup\$ Commented Jun 22, 2017 at 13:55

AltStyle によって変換されたページ (->オリジナル) /