ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.