ruby - Split sentence into words only using while loop -
in c++, can add null character in end differentiate between words. not how in ruby not want use splice method. here how proceeding forward with.
i = 0 split_array = [] while (i<sentence.length) puts (sentence.length) if (i+1 == " ")
another way (disregarding punctuation , case):
sentence = "the quick brown dog\njumped on lazy fox" start_ndx = sentence.index(/\w/) return [] if start_ndx.nil? words = [] while start_ndx end_ndx = sentence.index(/\w\b/, start_ndx) words << sentence[start_ndx..end_ndx] start_ndx = sentence.index(/\w/, end_ndx+1) end words #=> ["the", "quick", "brown", "dog", "jumped", "over", "the", "lazy", "fox"]
Comments
Post a Comment