Created
April 13, 2012 01:59
-
-
Save rares/2372894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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