I am trying to write a program which converts all uppercase characters in a string to lowercase and lowercase characters to uppercase.For example if the string is "abcdE", it will return after coversion "ABCDe". This is what I have tried so far
class String
def is_upper?
self == self.upcase
end
def is_lower?
self == self.downcase
end
end
s = gets.chomp
if s.length <= 100
a = s.split(//)
b, c = Array.new
a.each do |m|
if m.is_upper?
b.push(m.downcase)
end
if m.is_lower?
b.push(m.upcase)
end
end
c = b.join
end
However I can assess that this program is very erroneous. I am still a novice learner. So forgive me if my solution program is very wrong.Please help me to find a correct and easier solution.
No need to invent the wheel again.
There is already a method named swapcase in ruby to accomplish this.
For ex-
"Hello".swapcase #=> "hELLO"
"cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
You can find more details here-
http://ruby-doc.org/core-2.2.0/String.html#method-i-swapcase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With