Ruby Basic Exercises: Check a given string contains 'i' characters
Ruby Basic: Exercise-39 with Solution
Write a Ruby program to check a given string contains 'i' characters.
def test_str(str) count = 0 for i in 0..str.length do if(str.slice(i) == 'i') count = count + 1 end end return (count >= 1 && count <= 3); end print test_str('Python'),"\n" print test_str('Diligent1'),"\n" print test_str('Industrious'),"\n" print test_str('Impartiality')
false true true true
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check two positive integer values and return the larger value that is in the range 20..30 inclusive, or return 0 if no number is in that range.
Next: Write a Ruby program to create a new string taking every other character starting with the first of a given string.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework