Python | Finding ‘n’ Character Words in a Text File
This article aims to find words with a certain number of character. In the code mentioned below, a python program is given to find the words contain three characters in the text file.
Examples:
Input : Hello, how are you ? Output : how are you
Code: Python program to find the words contain three characters in the text file
PYTHON
count = 1 chrw = "" # text file file = open ( 'textfile.txt' , 'r' ) while 1 : sp = file .read( 1 ) if count< = 3 : chrw = chrw + sp if count> 3 : if sp = = " ": count = 0 if len (chrw)> 0 : print (chrw) chrw = "" elif sp ! = " ": chrw = "" count = count + 1 if not sp: break file .close() |
Output:
how are you
Time complexity: O(n), where n is the number of characters in the text file.
Auxiliary space: O(1).
Please Login to comment...