w3resource

Ruby Basic Exercises: Check whether a string starts with 'if'

Ruby Basic: Exercise-5 with Solution

Write a Ruby program to check whether a string starts with "if".

Ruby Basic Exercises: Check whether a string starts with 'if'

Ruby Code:

def start_if(str)
   return str[0, 2] == "if";
end
print start_if("ifelse"),"\n"
print start_if("endif"),"\n"

Output:

true
false

Flowchart:

Flowchart: Check whether a string starts with 'if'

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program which accept the radius of a circle from the user and compute the parameter and area.
Next: Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/basic/ruby-basic-exercise-5.php