brewersfan1976

longest_word.rb

Mar 5th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.50 KB | None | 0 0
  1. def longestWord(text)
  2.     array = Array.new
  3.     array_length = text.length
  4.     x = 0
  5.     y = 0
  6.     string = ""
  7.    
  8.     while (x < array_length)
  9.         if ((text[x] >= 'A' and text[x] <= 'Z') or (text[x] >= 'a' and text[x] <= 'z')) then
  10.             string = string + text[x]
  11.         else
  12.             array[y] = string
  13.             y = y + 1
  14.             string = ""
  15.         end
  16.            
  17.         x = x + 1
  18.     end
  19.        
  20.     array[y] = string
  21.        
  22.     return array.max_by(&:length)
  23. end
Advertisement
Add Comment
Please, Sign In to add comment