Ruby Basic Exercises: Create a new string taking every other character starting with the first of a given string
Ruby Basic: Exercise-40 with Solution
Write a Ruby program to create a new string taking every other character starting with the first of a given string.
def string_test(str) str1 = "" str.split("").each_with_index do |char, index| str1 += char unless index % 2 == 1 end return str1 end print string_test('abcdefgij'),"\n" print string_test('abcdefg'),"\n" print string_test('abcdef'),"\n" print string_test('abc'),"\n" print string_test('ab'),"\n"
acegj aceg ace ac a
Ruby Code Editor:
Contribute your code and comments through Disqus.
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