Skip to content

Commit 17cb1e8

Browse files
Merge pull request #222 from priyankadaryani/patch-1
Add a program to count the number of vowels in a text file.
2 parents f318e40 + da10b52 commit 17cb1e8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Count_Number_of_Vowels_in_a_File.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
######## This program counts and returns the number of vowels in a text file. ########
2+
3+
# Ask the user for the name of the file. The file should be in the same folder and should be a text file.
4+
print("Enter the Name of File: ")
5+
6+
# Convert the name to string and open the file in read mode
7+
fileName = str(input())
8+
fileHandle = open(fileName, "r")
9+
10+
# Declare a variable to store the number of vowels. Initally it is zero.
11+
count = 0
12+
13+
# create an array of all the vowels (upper and lower case) that can be used to compare and determine if a character is a vowel
14+
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
15+
16+
# Read each character and compare it to the characters in the array. If found in the vowels array, then increase count.
17+
for char in fileHandle.read():
18+
if char in vowels:
19+
count = count+1
20+
21+
# Close the file
22+
fileHandle.close()
23+
24+
# Print the count to the screen for the user to see.
25+
print("\nThe total number of vowels in the text are:")
26+
print(count)

0 commit comments

Comments
 (0)