CS396-Projects/wordcounter.rs

28 lines
No EOL
926 B
Rust

use std::io;
fn main()
{
// input from the user to get the number of sentences they'd like to enter
println!("How many sentences would you like to input?");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
let mut num_sentences: u32 = input.trim().parse().expect("Please enter a number");
let mut total_words = 0;
// loop that would take input for each sentence
// calculates the number of words
while num_sentences > 0
{
println!("Please enter a sentence:");
input.clear();
io::stdin().read_line(&mut input).expect("Failed to read line");
let num_words = input.split_whitespace().count();
println!("The sentences has {} words", num_words);
total_words = total_words + num_words;
num_sentences = num_sentences - 1;
}
println!("Total number of words: {}", total_words);
}