Question

Write a function called "wc", which stands for word count (a unix utility). wc takes one...

Write a function called "wc", which stands for word count (a unix utility). wc takes one string as input, which should be the name of a text file. wc prints on one line, the number of lines, the number of words, and the number of characters in the file (three numbers separated by tabs). Hint: Use word_count as a subroutine; and \t gives the tab character.

(The code is to be written in python)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query do comment. Please rate answer as well. Thanks

Code:

def wc(file):

    numberOfLetters =0

    numberOfWords = 0

    numberOfLines =0

    #read file line by line

    for line in file:

        #incremented the numberOfLines by 1

        numberOfLines +=1

        #split the line to get words

        words = line.strip().split()

        #update the count of words

        numberOfWords += len(words)

        #inside each word count the letters and update the count of letters

        for word in words:

            for letter in word:

                numberOfLetters +=1

    #print the output in one line seperated by tab

    print("{}\t{}\t{}".format(numberOfLines,numberOfWords,numberOfLetters))

#main driver program

file = open("input.txt")

wc(file)


========Screen shot of the code========

Output:

Add a comment
Know the answer?
Add Answer to:
Write a function called "wc", which stands for word count (a unix utility). wc takes one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT