Skip to content

Instantly share code, notes, and snippets.

@rares
Created April 13, 2012 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rares/2372894 to your computer and use it in GitHub Desktop.
Save rares/2372894 to your computer and use it in GitHub Desktop.
class String
def strstr(pattern)
0.upto self.size do |i|
matches = 0
0.upto pattern.size do |j|
if self[i + j] == pattern[j]
matches += 1
else
break
end
return i if matches == pattern.size
end
end
-1
end
end
puts "hello".strstr("el")
puts "hello".strstr("ll")
puts "hello".strstr("o!")
puts "hello".strstr("y")
puts "hello".strstr("hello")
puts "hello".strstr("x")
puts "hello".strstr(" ")
puts "hello".strstr("lo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment