Q:

Write a Ruby program to create a new string from a given string

0

Write a Ruby program to create a new string from a given string using the first three characters or whatever is there if the string is less than length 3. Return n copies of the string.

All Answers

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

Ruby Code:

def multiple_string(str, n)
    str.length < 3 ? str*n : str[0..2]*n
end

print multiple_string('abcdefg', 1),"\n"
print multiple_string('abcdefg', 2),"\n"
print multiple_string('abcdef', 1),"\n"
print multiple_string('abcdef', 2),"\n"
print multiple_string('abc', 1),"\n"
print multiple_string('ab', 2),"\n"
Output:
abc
abcabc
abc
abcabc
abc
abab

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