Challenge: write a code in any language to count newlines, words and characters in a text file. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Challenge: write a code in any language to count newlines, words and characters in a text file.

wc (short for word count) is a command in Unix-like operating systems. The program reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow. for example, in Unix systems, $ wc foo gives output as, 10 50 250 foo which means text file 'foo' has 10 newlines, 50 words and 250 characters. https://en.m.wikipedia.org/wiki/Wc_(Unix)

8th Oct 2017, 7:43 PM
Debraj Das
Debraj Das - avatar
1 Answer
+ 3
In Python should be something like this: lines = 0 words = 0 chars = 0 with open('file_name', 'r') as fp: for line in fp: wordsCount = line.split() lines += 1 words += len(wordsCount) chars += len(line) print(lines, words, chars)
8th Oct 2017, 10:26 PM
Leno
Leno - avatar