diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-07-19 21:54:35 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-07-19 21:54:35 +0300 |
commit | 0cef25cad181c03eb9e9e4b10f697aca1ccb74a5 (patch) | |
tree | 165b13278d209965fedd49916de05f4e1d93ac18 /src | |
download | wordfreq-0cef25cad181c03eb9e9e4b10f697aca1ccb74a5.tar.gz |
Add: write out all words in input
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b0d0d03 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,41 @@ +use std::env; +use std::fs::File; +use std::io; +use std::io::BufRead; + + + +fn main() -> io::Result<()> { + for filename in env::args().skip(1) { + let mut f = File::open(&filename)?; + let mut reader = io::BufReader::new(f); + print_words(&mut reader)?; + } + Ok(()) +} + + +fn print_words(reader: &mut io::BufReader<File>) -> io::Result<()> { + for line in reader.lines() { + let line = line?; + print_words_in_line(&line); + } + Ok(()) +} + + +fn print_words_in_line(line: &str) { + let mut was_in_word = false; + for c in line.chars() { + let letter = c.is_alphabetic(); + if letter { + was_in_word = true; + print!("{}", c); + } else { + if was_in_word { + println!(""); + } + was_in_word = false; + } + } +} |