Sunday, August 18, 2013

Coderbyte: Letter Changes (Ruby)

PROBLEM

Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.

SOLUTION

def LetterChanges(string)
  alphabet = ("a".."z").to_a + ("A".."Z").to_a
  new_string = ""
  placeholder = ""
  
  string.split(//).each do |letter|
    placeholder = letter
    alphabet.each_with_index do |character, index|
      if letter == "z"
        placeholder = alphabet[0]
      elsif letter == "Z"
          placeholder = alphabet[26]  
      elsif character == letter
        placeholder = alphabet[index+1]    
      end
    end
    new_string = new_string + "#{placeholder}"
  end
  
  return new_string.gsub(/[aeiou]/, &:upcase)      
end
   
print LetterChanges(STDIN.gets)