Ruby program to find occurrence of character in string



Ruby program to find occurrence of character in string

class CountCharOccurrence
  def countCharOccurrence
    print "Enter the string"
    input_string = gets.chomp.downcase
    print "Enter the search character"
    search = gets.chomp.downcase
    count = 0 
    input_string.split("").each do |s|
      count = count + 1 if s == search
    end
    print "character #{search} occurrerd #{count} times"
  end
end

p = CountCharOccurrence.new 
p.countCharOccurrence

Output:


Enter the string Umesh kumar kushwaha
Enter the search character u
character u occurrerd 3 times


No comments:

Post a Comment