How to count the white spaces in a string in Rust

1 Answer

0 votes
fn count_whitespace_characters(s: &str) -> usize {
    let mut count = 0;
    
    for ch in s.chars() {
        if ch.is_whitespace() {
            count += 1;
        }
    }
    
    count
}

fn main() {
    let s = "Rust \n  Programming \r Language \t ";
    
    println!("Total white spaces: {}", count_whitespace_characters(s));
}


      
/*
run:
  
Total white spaces: 10
 
*/
 

 



answered Oct 20, 2024 by avibootz

Related questions

1 answer 111 views
1 answer 123 views
1 answer 106 views
1 answer 106 views
1 answer 78 views
...