# Author: Scott Wehrwein
# Date: 11/22/2019
# Demo reading from and writing to files.

def rev_words(in_filename, out_filename):
    """ Write the contents of in_file to out_file, but with
        each word written in reverse order. """
    infile = open(in_filename, 'r')
    outfile = open(out_filename, 'w')
    
    # loop over lines of the file
    for line in infile:
        # newlines aren't remove automatically, so we do it here:
        line = line.strip('\n')
        line_words = line.split() # split the line on whitespace
        for word in line_words: # for each word
            # write the characters in reverse order:
            for i in range(len(word)-1, -1, -1):
                outfile.write(word[i])
            
            outfile.write(" ") # put a space between each word
        outfile.write('\n') # put a newline at the end of each line
    
    # close both files
    infile.close()
    outfile.close()


rev_words("test.txt", "llorkcir.txt")