EMPTY = "" NEWLINE = "\n" PUNCTUATION = "." SPACE = " " # Author: James Tam # Version: December 2, 2021 # Features: Program reads text from file and displays information about each line. # @analyzeWords(list) # @returns(nothign) # Features # * Steps through each word in the line (element of a tuple) # * Displays each word on its own line along with a word count # * The total number of words is shown. def analyzeWords(aRow): currentWordCount = -1 numWords = -1 charIndex = -1 sentenceLength = -1 charIndex = 0 numWords = 1 currentWordCount = 1 sentenceLength = len(aRow) while (charIndex < sentenceLength): while(aRow[charIndex] != SPACE): print(aRow[charIndex], end = "") if (aRow[charIndex] == SPACE): numWords = numWords + 1 charIndex = charIndex + 1 charIndex = charIndex + 1 print("\nNumber of words in the line=%d" %(numWords)) for i in range (1,20,1): print("=", end="") print() # @fileInput(none) # @returns(nothing) # Features: # * Opens the specified text file. # * Reads a line at a time (as a string) from the file # * The string is then split into words according to a space separator. # * The individual words are stored as elements of a list. def fileInput(): r = -1 c = -1 aChar = EMPTY aList = [] inputFile = open("words.txt","r") aLine = inputFile.readline() r = 0 while (aLine != EMPTY): aList.append([]) c = 0 while (aLine[c] != PUNCTUATION): aList[r].append(aLine[c]) c = c + 1 analyzeWords(aList[r]) aLine = inputFile.readline() r = r + 1 # @start(none) # @returns(nothing) def start(): fileInput() start()