๐Ÿ“œ
B.TECH III YEAR โ€ข II SEMESTER โ€ข R22

Scripting Languages - Lab Manual

Scripting Languages Laboratory โ€ข Program 1 Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š II Semester ๐Ÿ“œ Scripting Languages ๐Ÿ’Ž Ruby โ€ข TCL โ€ข Perl ๐Ÿงช Lab Program
Aim of Program 1

Write a Ruby script to create a new string which is n copies of a given string where n is a non-negative integer.

โ† All Programs
Solution

Aim:

☛   Write a Ruby program to create a new string which is n copies of a given string where n is a non-negative integer.

Solution :

PROGRAM: (str_n_copies.rb)

 
def string_copies(str, n)
  return str * n    # returns n copies of str      
end

print "Enter a string: "
input_str = gets.chomp

print "Enter a non-negative integer: "
n = gets.to_i

result = string_copies(input_str, n)
puts "Result: #{result}"


OUTPUT:

 
Enter a string: StudyGlance
Enter a non-negative integer: 4
Result: StudyGlanceStudyGlanceStudyGlanceStudyGlance



โ† Back to Scripting Languages Lab Programs ๐Ÿ“š All Lab Manuals