Rust program to check whether a given character is a whitespace character or not

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

Problem Solution:

Here, we will create two-character variables and check created variable contains a whitespace character or not.

Program/Source Code:

The source code to check a given character is a whitespace character or not is given below. The given program is compiled and executed successfully.

// Rust program to check whether a given character 
// is a whitespace character or not

fn main() {
    let mut ch:char = ' ';
    let mut ch1:char = 'B';
    
    if ( (ch == ' ' ) || 
         (ch == '\t') || 
         (ch == '\n') || 
         (ch == '\r')  
        )
    {
       println!("Variable ch contains whitespace character");
    }
    else
    {
       println!("Variable ch does not contain whitespace character");
    }
    
    if ( (ch1 == ' ' ) || 
         (ch1 == '\t') || 
         (ch1 == '\n') || 
         (ch1 == '\r') 
        )
    {
       println!("Variable ch1 contains whitespace character");
    }
    else
    {
       println!("Variable ch1 does not contain whitespace character");
    }
}

Output:

Variable ch contains whitespace character
Variable ch1 does not contain whitespace character

Explanation:

Here, we created two character variables ch and ch1 that are initialized with ' ' and 'B' respectively. Then we checked the given characters contains whitespace characters 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.