Page 525 - Computer Science V2.0 Class 11
P. 525

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')


                     def countDigits(string):
                         '''
                         Objective : To count the number of digits in the string
                         Input Parameter : string

                         Return Value : sum – numeric value
                         '''
                         return sum(1 for char in string if char.isdigit())



                     def replaceSpaces(string):


                                                                                                           Practical  511
   520   521   522   523   524   525   526   527   528   529   530