Question: Write a program that reads a content of a text 2 . txt ( file under module ) . The program should create a dictionary

Write a program that reads a content of a text2.txt (file under module). The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word 'the' appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value.
Define Main ()
Create an empty dictionary
Prompt user to get the file
Open file in R mode
Use read method to read data from the file and split method to split the words
Add each unique word to dictionary with a counter of 0 using a for loop
For each word in the text increase its counter in the dictionary
Display results
Expected output:
Enter the name of the input file: text2.txt
word occurrences
----------------------------------------------
were 1
this 2
In 2
at 1
biggest 1
assets 2
wheels 2
400-horsepower 1
famous 1
the text2.txt file contains the following
"No one is unaware of the name of that famous English shipowner, Cunard.
In 1840 this shrewd industrialist founded a postal service between Liverpool and Halifax, featuring three wooden ships with 400-horsepower paddle wheels and a burden of 1,162 metric tons.
Eight years later, the company's assets were increased by four 650-horsepower ships at 1,820 metric tons, and in two more years, by two other vessels of still greater power and tonnage.
In 1853 the Cunard Co., whose mail-carrying charter had just been renewed, successively added to its assets the Arabia, the Persia, the China, the Scotia, the Java, and the Russia, all ships of top speed and, after the Great Eastern, the biggest ever to plow the seas.
So in 1867 this company owned twelve ships, eight with paddle wheels and four with propellers."
Write a program that reads a content of a text 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!