Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Assignment No: 7

Title:
To count total characters in file, total words in file, total lines in file and frequency of
given word in file
Aim:
To count total characters in file, total words in file, total lines in file and frequency of
given word in file.
Theory:
Counting the number of characters is important because almost all the text boxes that rely on
user input have a certain limit on the number of characters that can be inserted.
Algoritm:
Step1: Start
Step2: Input the file name to be used for calculation
Step3: a) char_count #This counts all the characters in given file including in and \t
Print char_count
b) word_count = len(contents.split())-1 # Here contents will be split on all white
spaces including \n, \t
Print word_count
c) line_count = len(contents.split("."))-1 #Each sentence ends with . so split on. will
give sentences
Print line_count
Step4: match = input("Enter word to be found in the contents..")
contents = contents.lower() # make all words in lowercase so that match is not case sensitive
match_count = len(contents.split(match)) - 1 #split contents based on the given word...
print("No of time",match, "occurs is ", match_count)

Step5: Stop
Conclusion:
By using char_count, word_count, line_count we calculated the number of lines, words,
characters and word present or not in a sample text file by using python programming language.

You might also like