Rust program to check whether the given character is alphanumeric or not

Given a character, we have to check whether the given character is alphanumeric or not using Rust program.
Submitted by Nidhi, on September 28, 2021

Problem Solution:

Here, we will create two-character variables and check created characters contains an alphanumeric value or not.

Program/Source Code:

The source code to check whether the given character is alphanumeric or not is given below. The given program is compiled and executed successfully.

// Rust program to check the given character 
// is alphanumeric or not

fn main() {
    let mut ch:char = 'A';
    let mut ch1:char = '#';
    
    if ( (ch >= '0' && ch <= '9') ||
         (ch >= 'a' && ch <= 'z') || 
         (ch >= 'A' && ch <= 'Z')
       )
    {
       println!("Character '{}' is alphanumeric",ch);
    }
    else
    {
        println!("Character '{}' is not alphanumeric",ch);
    }
    
    if ( (ch1 >= '0' && ch1 <= '9') ||
         (ch1 >= 'a' && ch1 <= 'z') || 
         (ch1 >= 'A' && ch1 <= 'Z')
       )
    {
       println!("Character '{}' is alphanumeric",ch1);
    }
    else
    {
        println!("Character '{}' is not alphanumeric",ch1);
    }
}

Output:

Character 'A' is alphanumeric
Character '#' is not alphanumeric

Explanation:

Here, we created two characters variables ch and ch1 that are initialized with 'A' and '#' respectively. Then we checked the given characters are alphanumeric or not and printed the appropriate message.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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