[Ruby] Search problem using index method

I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.

problem

Use the index method to create a method that returns the number of characters "qiita" appears from the left in any string.

Output example:

find_qiita("xxxqiitaxxx") → 4 find_qiita("qiitaxxxxxx") → 1 find_qiita("qitaxxqiita") → 7

answer

def find_qiita(str)
  puts str.index("qiita") + 1
end

Commentary

The index method returns a number counting from 0 at the beginning of the string, so you need to do + 1.

Recommended Posts

[Ruby] Search problem using index method
ruby search problem
Ruby problem ⑦
[Ruby] Obtaining even values ​​using the even? Method
Ruby to_s method
[Ruby] FizzBuzz problem
Binary search binary search method
ruby API problem
Method to search
scan method problem
[Ruby] end_with? method
[Ruby] Method memorandum
[Ruby] Ruby API problem
Binary search method
ruby API problem
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Arbitrary search method in an array using binary search
[Ruby] Cut out a string using the slice method
Ruby Learning # 30 Initialize Method
[Ruby] Implement error handling / iteration processing using while method
abbreviation for ruby method
Examine the elements in the array using the [Ruby] includes? Method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
definition of ruby method
Training using each_with_index method
[Ruby] Method definition summary
Try using each_with_index method
[Ruby on Rails] Search function (model, method selection formula)
[Ruby] Searching for elements in an array using binary search
Linear search problem for arrays
Practice of binary search method
Creating a calendar using Ruby
Integer check method with ruby
Ruby algorithm (inject, method definition)
Statically typed Ruby using Sorbet
Create a fortune using Ruby
[Ruby] Notes on gets method
[Ruby] problem with if statement
Practice of linear search method
Try using gRPC in Ruby
[Ruby] Method that returns truth
[Programming Encyclopedia] §2 Try using Ruby
Ruby deposit system, algorithm problem
[ruby] Method call with argument