#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Need help with a Ruby assignment


    Here is the assignment directions:

    In this program you need to create a letter counter. A user is first asked to enter a string. After that, the user is asked to enter a character. The program should count and display the number of times that the specified character appears in the string.

    Unfortunately, I do not know where to start if someone could give me a hand?

    I would appreciate any help given!

    -Max
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2004
    Location
    Central Washington (USA)
    Posts
    518
    Rep Power
    11
    I shouldn't be doing your homework for you, but I'm bored and thinking about ruby.
    Code:
    def run
        puts 'Enter a string to search'
        text = gets.chomp!
        puts 'Enter a character to search for'
        target = gets.chomp!
        count = text.scan(/#{target}/).count
        puts "Found #{count} occurrences of the character #{target}"
    end
    run

IMN logo majestic logo threadwatch logo seochat tools logo