Question

Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words).

Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers:

  • The count of lines/sentences
  • The count of words
  • The count of characters
  • Do not count blank spaces in the file.
  • use payton program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

Output:

filename.txt file:

Raw Code:

filename = input("Enter file name: ") # Taking filename from user.Before running this program make sure that the given
                                   # file is exist in the current directory otherwise it will give an error.
file = open(filename,"r") # opening a file.
lines = file.readlines() # reading lines from file.
lines_count = len(lines) # len(lines) will be the no of lines in a file.
words_count = 0 # initializing variables.
chars_count = 0 # initializing variables.
for line in lines: # iterate through each line in lines.
   words = line.split() # spliting line based on spaces and new lines.
   words_count = words_count + len(words) # the len(words) will be added to words_count
   for word in words: # iterate through each word in words
       chars_count = chars_count + len(word) # adding len(word) to chars_count variable
# print statements.
print("The count of lines/sentences:",lines_count)
print("The count of words:",words_count)
print("The count of characters:",chars_count)

If you have any doubts feel free to comment in comment section.

I HOPE THIS WILL HELP TO YOU.

DO VOTE(LIKE).

Add a comment
Know the answer?
Add Answer to:
Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...
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
  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • Linux & Unix Write a bash program to indent the code in a bash source file....

    Linux & Unix Write a bash program to indent the code in a bash source file. Conditions:     The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input.     The source file will not contain comments (words beginning with #). Requirements:     Read from standard input, write to standard output.     Code inside while statements should be indented 2 spaces. Be sure your program includes all of the following:    ...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE...

    WRITE A CODE IN C++ A CLASS WITH MEMBER FUCNTIONS THAT COUNTS THE LINES OF CODE OF C++ FILE OR TEXT FILE .COMMENTS SHOULD NOT BE COUNTED AND SPACE BETWEEN LINES OF CODE SHOULD BE IGNORED AS WELL. ASK FOR INPUT FROM THE USER IT CAN BE CPP FILE OR TEXT FILE Assignment:Write a program to count lines of code according to your LOC Counting Standard. Notes: -Your program should accept as input the name of a file that contains...

  • Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically,...

    Description: Overview: You will write a program (says wordcountfreq.c) to find out the number of words and how many times each word appears (i.e., the frequency) in multiple text files. Specifically, the program will first determine the number of files to be processed. Then, the program will createmultiple threads where each thread is responsible for one file to count the number of words appeared in the file and report the number of time each word appears in a global linked-list....

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
ADVERTISEMENT