Page 446 - Computer Science Class 11 With Functions
P. 446

for char in string:

                     if char.isalpha():
                         alphabetCount += 1
                     elif char.isdigit():
                         digitCount += 1

                     else:
                         specialCount += 1
                 return alphabetCount, digitCount, specialCount


             string = input("Enter a string: ")
             alphabetCount, digitCount, specialCount = countCharacters(string)

             print(f"Alphabet count: {alphabetCount}")
             print(f"Digit count: {digitCount}")
             print(f"Special character count: {specialCount}")
        Program 34

        Write a menu-driven program that accepts  a string and depending on the user's choice displays the following:
        1.  length of the string

        2.  number of  occurrences of 'm' it contains
        3.  number of digits it contains
        4.  the string obtained on replacing each space in it by  '*'

        5.  the string obtained on replacing each alphabet in uppercase by the corresponding alphabet in lowercase
        6.  the corresponding string in title case
        Note: Use Built-in functions for each menu option.

        Ans. def stringLength(string):
                 '''
                 Objective : To calculate the length of the string

                 Input Parameter : string
                 Return Value : len(string) – numeric value
                 '''
                 return len(string)



             def mCountOccurrence(string):
                 '''
                 Objective : To count the occurrences of 'm' in the string
                 Input Parameter : string
                 Return Value : numeric value

                 '''
                 return string.count('m')




         444   Touchpad Computer Science-XI
   441   442   443   444   445   446   447   448   449   450   451