Home » Ruby programming » Ruby programs

Ruby program to convert the string into lowercase and uppercase

Converting string into lowercase and uppercase: Here, we are going to learn how to convert a given string into lowercase and uppercase in Ruby programing language?
Submitted by Hrithik Chandra Prasad, on August 10, 2019

Converting string into lowercase and uppercase

Given a string and we have to convert it into lowercase and uppercase.

Lowercase or uppercase of a string can be easily printed or stored using upcase or downcase predefined methods. Using this method makes the code less complex but if you want to get through internal processing you should opt for method 2 where we have to find the uppercase or lowercase character of the specified character using its ASCII value.

Methods used:

  • puts: This method is used to put a string on the screen.
  • gets: This method is used to take input from the user.
  • .size: .size method returns the size of the string.
  • .ord: This is a method which is used to convert the character into its ASCII equivalent.
  • .chr: .chr is used to convert the ASCII value into its character equivalent.
  • .upcase: This simply converts the string into uppercase. It capitalizes each alphabet in the string.
  • .downcase: This simply converts the string into its lowercase.

Ruby code to convert a string into uppercase and lowercase

=begin 
Ruby program to convert a string into uppercase and lowercase
=end

# input the string
puts "Enter the string"
str=gets.chomp

#ask for the option 
puts "Choose the option: a)to lowercase b)to uppercase"
choice=gets.chomp

# condition to execute code based on the user's choice
if(choice=="a")
	i=0
	while(i!=str.size)
	    k=str[i].to_s
	    if(k>='A' and k<='Z')
	        str[i]=(k.ord+32).chr
        else
            str[i]=k
        end
	    i+=1
	end
	puts "The lowercase is #{str}"
else
	i=0
	while(i!=str.size)
	    k=str[i].to_s
	    if(k>='a' and k<='z')
	        str[i]=(k.ord-32).chr
	    else
	        str[i]=k;
	    end
	    i+=1
	end
	puts "The uppercase is #{str}"
end

Output

RUN1:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
a
The lowercase is hello world, how are you?

RUN2:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
b
The uppercase is HELLO WORLD, HOW ARE YOU?

In method 1, we are manipulating each alphabet using its ASCII value. The task gets a little complex but it is better to develop something from scratch for a great understanding of logic and concepts.

Method 2:

=begin 
Ruby program to convert a string into 
uppercase or lowercase.
=end

# input the string
puts "Enter the string"
str=gets.chomp

#ask for the option 
puts "Choose the option: a)to lowercase b)to uppercase"
choice=gets.chomp

# condition to execute code based on the user's choice
if(choice=="a")
	str=str.downcase
	puts "The lowercase is #{str}"
else
	str=str.upcase
	puts "The uppercase is #{str}"
end

Output

RUN1:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
a
The lowercase is hello world, how are you?

RUN2:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
b
The uppercase is HELLO WORLD, HOW ARE YOU?





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.