Shuffle letters of a sequence of wodrs except for the first and last letters.
#!/usr/bin/env ruby
#
# Shuffle letters of a sequence of wodrs except for the first
# and last letters.
#
def isalpha(word)
if ("a" .. "z") === word[0] || ("A" .. "Z") === word[0]
return true
end
return false
end
def scramble(word)
if (word.length <= 3)
return word
end
return word[0] +
word[1 .. word.length - 2].split(//).shuffle().join() +
word[word.length - 1]
end
loop {
line = gets()
if line == nil
break
end
tokens = line.chomp().split(/( |\[|\]|\(|\)|\"|\.|\?|\!|\/|\\|\:|\,)/)
tokens.each do |token|
if isalpha(token)
token = scramble(token)
end
printf("%s", token)
end
puts
}