Advertisement
phreeak

Count Letters recursive

Mar 5th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.36 KB | None | 0 0
  1. def count_occurrences(t,l)
  2.   if t == "" then
  3.     return 0
  4.   else
  5.     if t[0,1] == l then
  6.       return 1 + count_occurrences(t[1,t.length - 1], l)
  7.     else
  8.       return count_occurrences(t[1,t.length - 1], l)
  9.     end
  10.   end
  11. end
  12.  
  13. #s = "hallo"
  14. #puts(count_occurrences(s,'l'))
  15.  
  16. test = gets().chomp
  17. letter = gets()[0,1]
  18.  
  19. puts(count_occurrences(test,letter))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement