# L-22 MCS 260 Wed 17 Oct 2007 : count words # # This program counts the number times # the sequence 'the' occurs in a text. # def add_to_buffer(b,c): "adds c to a 3-letter buffer b" B = b if len(b) == 3: B[0] = B[1]; B[1] = B[2]; B[2] = c else: B.append(c) return B search = ['t','h','e'] buffer = [] name = raw_input('give file name : ') file = open(name,'r') cnt = 0 while True: c = file.read(1) if c == '': break buffer = add_to_buffer(buffer,c) if buffer == search: cnt += 1 file.close() print '#occurrences of the : %d' % cnt