Q:

Write a Ruby program to check a given string contains 'i' characters

0

Write a Ruby program to check a given string contains 'i' characters.

All Answers

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

def test_str(str)
    count = 0
    for  i in  0..str.length do
    	if(str.slice(i) == 'i')
       		count = count + 1
		end
	  end
	return (count >= 1 && count <= 3);
end

print test_str('Python'),"\n"
print test_str('Diligent1'),"\n"
print test_str('Industrious'),"\n"
print test_str('Impartiality')

Output:

false
true
true
true

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now