CBSE · 083Class XII · 2024Section C3 marks

Question 28(or)

2 April 2024 · Computer Science (083)

Write a function, c_words() in Python that separately counts and displays the number of uppercase and lowercase alphabets in a text file, Words.txt.

Answer

python
def c_words():
    f = open("Words.txt", "r")
    Txt = f.read()
    CUpper = 0
    CLower = 0
    for ch in Txt:
        if ch.isupper():
            CUpper += 1
        elif ch.islower():
            CLower += 1
    print("Uppercase:", CUpper, "Lowercase:", CLower)
    f.close()

Explanation

Iterates the file content using isupper() and islower() to count characters of each case.