[RUBY] Count letters

Create a method that counts the number of specified characters in an arbitrary character string and outputs that number.

Let's create it depending on how many letters "go" are.

It seems that the scan method can be used. The scan method is a method that counts the specified character string from the target element and returns it as an array. reference: Ruby 3.0.0 Reference Manual, scan

def count_go(str)
  puts str.scan("go")
end

Now you can get "go" as an array. Finally, there is a description to get the number.

def count_go(str)
  puts str.scan("go").length
end

Now you can output in numbers.

Recommended Posts

Count letters
ruby Uppercase letters
Handle letters ⇔ numbers