# L-22 MCS 260 Wed 17 Oct 2007 : locword # # This program prints the locations in file # at each occurrent of the word 'the'. # def add_to_buffer(b,c): "adds c to 3-letter buffer" 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') locations = [] while True: c = file.read(1) if c == '': break buffer = add_to_buffer(buffer,c) if buffer == search: locations.append(file.tell()-3) file.close() print 'the occurs at ', locations