Ruby program to iterate characters using the each_char() function

Ruby Example: Write a program to iterate characters using the each_char() function.
Submitted by Nidhi, on January 06, 2022

Problem Solution:

In this program, we will create a string and iterate the characters of the string using the each_char() function and print the character of the string.

Program/Source Code:

The source code to iterate characters using each_char() function is given below. The given program is compiled and executed successfully.

# Ruby program to iterate characters of a 
# string using each_char() function

str = "Hello how are you";

str.each_char { |ch| print ch," " };

Output:

H e l l o   h o w   a r e   y o u

Explanation:

In the above program, we created three strings str initialized with "Hello how are you". Then we iterated the string characters and printed them.

Ruby Strings Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.