Ruby program to check the input string contains numbers or letters using case statement

Ruby Example: Write a program to check the input string contains numbers or letters using case statement.
Submitted by Nidhi, on December 17, 2021

Problem Solution:

In this program, we will read a string from the user and check the input string containing numbers or letters using the case statement.

Program/Source Code:

The source code to check input string containing numbers or letters using the case statement is given below. The given program is compiled and executed successfully.

# Ruby program to check the input string contains 
# numbers or letters using case statement

print "Enter string: ";
str = gets.chomp;  

puts case 
when str.match(/\d/)
    "Input string contains numbers";    
when str.match(/[a-zA-Z]/)
    "Input string contains letters";    
else
    "Invalid choice";    
end

Output:

Enter string: 1234
Input string contains numbers

Explanation:

In the above program, we read a string from the user and check the input string contains numbers or letters using the case statement, and print the appropriate message.

Ruby case Statement Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.