122k views
2 votes
Write a program to read in a file and count how many times any two particular words occur in succession

User Yaroslawww
by
4.3k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The following code is written in Python, it is a function that takes in the file location and two words as arguments. Then it reads the file and loops through to see if those two words are found in succession and how many times. Finally it returns the number of times those words are found in succession within the file.

def countSuccession(file, word1, word2):

file = open(file, "r")

words = file.read()

words = words.replace('.', '')

words = words.split(' ')

count = 0

for index in range(len(words)):

if words.__getitem__(index) == word1 and words.__getitem__(index + 1) == word2:

count += 1

return count

Write a program to read in a file and count how many times any two particular words-example-1
User Robin Westerlundh
by
4.8k points