Q:

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

belongs to collection: Rust Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to check whether a given character is... >>
<< Rust program to check whether a character variable...