# L-17 MCS 260 Fri 19 Feb 2010 : 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, on return is the updated buffer. """ B = b if len(b) == 3: (B[0],B[1],B[2]) = (B[1],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