A
A
AlexILichev2021-07-28 12:22:56
ruby
AlexILichev, 2021-07-28 12:22:56

How to count letters in a string and display a list of letters and their number in a sentence?

str = 'the quick brown fox jumps over the lazy dog'
arr = str.split(' ')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-07-28
@AlexILichev

str
  .delete(' ')
  .split('')
  .reduce(Hash.new(0)){|acc, n| acc[n] += 1; acc}
  .each{|k, v| puts "#{k}: #{v}"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question