Skip to content

Instantly share code, notes, and snippets.

@GeorgiPachov
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgiPachov/84458fb5d82258d0e12b to your computer and use it in GitHub Desktop.
Save GeorgiPachov/84458fb5d82258d0e12b to your computer and use it in GitHub Desktop.
Implement a `wc` functionality using java.

Implement a wc functionality using java.

Are you familiar with the wc (word count) command? Here is what it does:

$ wc test.txt -l
33 //number of lines in test.txt

$ wc test.txt -w
165 //number of words in test.txt 

$ wc test.txt -c 
1730 //number of characters in test.txt

$ wc test.txt
33 165 1730 //lines, words, characters

Implement this subset of 'wc' in a java program.

Example usage of your program:

$ java -jar yourProgram.jar test.txt -l
33

$ java -jar yourProgram.jar test.txt -w
165

$ java -jar yourProgram.jar test.txt -c
1730

$ java -jar yourProgram.jar test.txt
33 165 1730

You are permitted to use Java 8 streams and lambdas.

Bonus:

Output the stats in JSON format:

java -jar yourProgram.jar test.txt
{
  "lines":33,
  "words":165,
  "chars":1730
}
Hidden objective:

We will test your program only on ASCII files. Can you think of an optimization you can use here in one of the cases? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment