×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

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

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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