Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Tuesday, November 24, 2020

Python program to read a text file and display the number of consonants/ uppercase/ lowercase characters in the file by G Krishna Chauhan

Source Code


def countCharacterType(str): 

vowels = 0

consonant = 0

specialChar = 0

digit = 0

for i in range(0, len(str)): 

ch = str[i] 

if ( (ch >= 'a' and ch <= 'z') or

(ch >= 'A' and ch <= 'Z') ): 

ch = ch.lower() 

if (ch == 'a' or ch == 'e' or ch == 'i'

or ch == 'o' or ch == 'u'): 

vowels += 1

else: 

consonant += 1

elif (ch >= '0' and ch <= '9'): 

digit += 1

else: 

specialChar += 1

print("Vowels:", vowels) 

print("Consonant:", consonant) 

print("Digit:", digit) 

print("Special Character:", specialChar) 

# Driver function. 

f=open("data.txt")

str=f.read()

countCharacterType(str) 




OUTPUT


data.txt : this is python programming.

Vowels: 6

Consonant: 17

Digit: 0

Special Character: 4

No comments:

Post a Comment