Question: A start up program is provided, which has a MyString class. This class has one data attribute __myString, and two methods. countWord method calculates how
A start up program is provided, which has a MyString class. This class has one data attribute __myString, and two methods.
countWord method calculates how many words in __myString
findMostFrequentChar method finds the most frequent character in __myString (case insensitive)
Main method is already provided. You need to implement countWord method and findMostFrequentChar method in MyString class.
**For reference:** Python language
class MyString:
def __init__(self, mystring): self.__myString = mystring
def getmystring(self): return self.__myString def setmystring(self, mystring): self.__myString = mystring def countWord(self): try: listInfo=[] file= open (stringFile,'r') for line in file: listInfo.append(line) except ValueError: print("value cannot be converted to float") except IOError: print("Error reading/opening file") except: print("An error has occured") return len(listInfo.split()) #return number of words in a sentence ## return len(nltk.word_tokenize(self.myString)) ## second way.
def findMostFrequentChar(self): string = open('TextFile.txt','r') # store frequent character in fc fc = " # store frequency of the frequent character in nfc nfc= 0 #run loop for each chaacter in the string for ch in string: # initialte the variable i= 0 # soters teh character count in a string j= 0 # iterating the loop tills the condition whilei if ch.lower()== string[i].lower():# comparing making all char to lower j+=1 i+=1 if j>nfc and ch!='': nfc=j fc=ch return fc def main(): try: info= input("Please enter anything you like: ") stringFile =open('TextFile.txt','w') stringFile.write(info) stringFile.close() except IOError: print("Error opening/writing file") except Exception as err: print(err) InfoFile = MyString("TextFile.txt") wordcount = InfoFile.countWord() print("The words you entered in",info, "is", wordcount) Fc= stringFile.findMostFrequentChar() print("The most frequent character is", Fc)
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
