Join the IDNLearn.com community and get your questions answered by experts. Join our community to access reliable and comprehensive responses to your questions from experienced professionals.

Write a program to read in a file and count how many times any two particular words occur in succession

Sagot :

Answer:

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

View image Sandlee09