Ruby program to reverse string without using direct method



Write a Ruby program to reverse string without using direct method

class ReverseString
  def reverseString
    print "Enter the string: "
    input_string = gets.chomp
    reverse_string = ""
    (0..(input_string.length - 1)).reverse_each do |i|
      reverse_string = reverse_string + input_string[i]
    end
    puts "Reverse string is: #{reverse_string}"
  end
end

p = ReverseString.new 
p.reverseString

Output:

Enter the string:  umesh kumar kushwaha
Reverse string is: ahawhsuk ramuk hsemu


No comments:

Post a Comment