8.4 Exercises

  1. Provide code using the split method to separate the following string into individual words and then display them:

    my_string = "Roses!are!red!Violets!are!blue"
  2. You are programming part of the sign-up procedure for an online computer game. Write a program that prompts the user to enter his or her user ID and password and enforces the following restrictions:

    1. User IDs and passwords must be at least seven characters in length.

    2. Passwords must contain at least one of each of the following: uppercase character, lowercase character, number, and symbol.

    Hint: use the documentation for the string class.

  3. Write a program that prompts the user for three words and then outputs the total and average number of characters in the three words.

  4. What would result if you were to type the following into irb?

    irb(main):001:0> "12345".length
    irb(main):002:0> "12345" * 5
    irb(main):003:0> "12345".index(2)
    irb(main):004:0> "12345".index(0)
    irb(main):005:0> "12345".index(1)
    irb(main):006:0> "12345".index(5)
  5. What does it mean to pass a parameter to a method? What happens when a parameter is passed to a method?

  6. Write a function multiplier that multiplies the value passed to it by x. Have the function ask the user for the value of x.

Get Computer Science Programming Basics in Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.