Write a program that inputs several(three) lines of text and a search string, and determines the total occurrences of the string in the lines of text.
def countOccurences(s, word):
a = s.split(" ")
count1 = 0
for i in range(0, len(a)):
if (word == a[i]):
count1 = count1 + 1
return count1
f = open('my_file.txt', 'r+')
serachWord=input("Enter a word to search: ")
count2=0;
for line in f.readlines():
count2+=countOccurences(line,serachWord)
print (count2)
f.close()
Comments
Leave a comment