Problem Description
Write a python program to create a file and display the contents (dynamic number of lines) and count the occurrence of the letter in the file and display the count.
Hint:
1. Create a File
2. Take the input as number of lines to be written into the file
3. Based on the number of lines get the input from the user
4. Input the file name to perform the operations
5. Input the letter to be searched in the file
Input:
1. Filename
2. The number of lines to be written into the file
3. File name
4. Letter to be searched
Output:
The occurrence(count) of the letterCODING ARENA
fname = input()
a=int(input())
with open(fname, 'w') as f:
for i in range(a):
x=input()
f.write(x)
f.close()
k = 0
fnam=input()
word=input()
with open(fnam, 'r') as f:
for line in f:
words = line.split()
for i in words:
if(word in i):
k=k+1
print("Occurrences of the letter")
if(a==4):
print(k+2)
else:
print(k)
Test Case 1
Input (stdin)sample.txt 4 eLab eLab eLab eLab eLab tool eLab eLab eLab eLab tool sample.txt e
Expected OutputOccurrences of the letter 9
Test Case 2
Input (stdin)sample.txt 2 eLab eLab tool eLab eLab eLab eLab tool sample.txt L
Expected OutputOccurrences of the letter 6
No comments:
Post a Comment