Q:

Ruby program to get characters from a string using the index

belongs to collection: Ruby Strings Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to get a substring from the string... >>
<< Ruby program to find the length of a string...