Answered step by step
Verified Expert Solution
Question
1 Approved Answer
a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether
a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether or not word is in the file named fileName. (Hint: Read the file in single shot into a strings and feel free to use the the membership test operator in: word in s) Test it on the file named "test.txt" consisting of: This is a test for Problem 3 of Programming Assignment 3 Test program: print (isWordInFile("test.txt", "Programming")) print (isWordInFile("test.txt", "programming")) b) Find word in text file: return line number. This is a variation of Part (a) which requires reading the file line by line. Implement the function wordSearch (fileNane, word), which given a string fileName and a string word (not containing the new line character), searches for word in the file named fileName. If found, it should return the line number of the first occurrence. Otherwise, it should return 0. Feel free to use the membership test operator in for strings. Test it on the file "test.txt" given in Part (a). Test program: print (wordSearch ("test.txt", "Programming")) print (wordSearch ("test.txt","programming")) Output: True False c) Duplicate lines. Implement the function duplicateLines (fileName), which given a string fileName, opens the file name fileName for reading and creates a new file whose content is like fileName but with all lines duplicated. This function assumes that extension of fileName is ".txt", i.e., as a string, fileName is of the form name.txt, for some string name. The new file should be called nameDuplicated.txt. Use assert to stop the program if the extension of fileName is not "txt". Display an appropriate error message. This is a test This is a test for for Output: 3 Test your function on the file "test.txt" given in Part (a). It should create a new file named "test Duplicated.txt" consisting of: Problem 3 of Programming Assignment 3 Problem 3 of Programming Assignment 3 Test it also a file whose extension is not ".txt".
Step by Step Solution
★★★★★
3.32 Rating (146 Votes )
There are 3 Steps involved in it
Step: 1
a Find word in text file def isWordInFilefilesearchstring with openfile ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started