Ruby program to get characters from a string using the index

Ruby Example: Write a program to get characters from a string using the index.
Submitted by Nidhi, on December 30, 2021

Problem Solution:

In this program, we will create a string variable, and then we will get the characters from the string based on the index.

Program/Source Code:

The source code to get characters from a string using the index is given below. The given program is compiled and executed successfully.

# Ruby program to get characters from 
# a string using the index.

Mystr = "Hello World";

index=0;

while index<Mystr.length
    print Mystr[index]," ";
    index=index + 1;
end

Output:

H e l l o   W o r l d

Explanation:

In the above program, we created a string variable Mystr initialized with "Hello World". Then we traversed the string and get characters from the string one by one using index and printed them.

Ruby Strings Programs »




Comments and Discussions!

Load comments ↻





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