Ratetod text fexir

GolfScript, 68 67 57 characters

" "/{..+{95&"AEIOUY"?)}:C,1>\{.C!!{96&\(31&@|}*}%\;}%" "*

Input must be given on STDIN. Example (online):

> You!!! I'm a badly written text. Some lunatic guy played with the order of vowels. Please fix me !
Ouy!!! I'm a bydla wrettin text. Semo lanituc gyu plyead with the erdor of vewols. Plaese fix me !

Ruby, 91 90 characters

$><<gets.gsub(/\S+/){|x|x.gsub(r=/[aeiouy]/i){|y|z=$'[r]||x[r];y>?Z?z.downcase: z.upcase}}

Coffeescript, 148 charectars

alert prompt().replace /\S+/g,(x)->x.replace r=/[aeiouy](?=(.*))/gi,(y,z)->((t=z.match r)&&t[0]||x.match(r)[0])[['toLow','toUpp'][y>'Z']+'erCase']()

No ussimtoans, except that "holf-cerract" is a word, and not my sulitoon.

Test:

> To test this program, simply enter a long sentence that appears half-broken
  To test this pragrom, sympli enter a long sentence that eppaars holf-brekan

> No MaTtEr HoW mUcH cAsE yOu uSe, It WoRkS
  No MeTtAr HoW mUcH cEsA oUy eSu, It WoRkS

Haskell, 159 characters

Didn't end up quite as short as I'd hoped, but I decided to post it anyway:

import Data.Char
f s=let(v,t)=foldr g(v,[])s in t
g x(v,t)=last$(v,x:t):[(x,c v:t)|c<-[toUpper,toLower],x`elem`map c"AEIOUY"]
main=interact$unwords.map f.words

Ungolfed version:

import Data.Char

rotateVowels :: String -> String
rotateVowels word = result
  where (vowel, result) = foldr step (vowel, []) word
        step ch (vowel, rest)
          | ch `elem` "AEIOUY" = (ch, toUpper vowel : rest)
          | ch `elem` "aeiouy" = (ch, toLower vowel : rest)
          | otherwise          = (vowel, ch : rest)

main = interact $ unwords . map rotateVowels . words

This is a nice example of how lazy evaluation can let you do cool tricks. In rotateVowels, each time a new vowel is found, it is passed on to the left, while replacing it with the one coming from the right. This part of the output from the fold is fed back as the input so that the leftmost vowel becomes the replacement for the rightmost one.

(vowel, result) = foldr step (vowel, []) word
 ^^^^^                        ^^^^^
 this part of the output      is part of the input

Tags:

Code Golf