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

v.   21
                       vi.  ['WZ-1', 'New Ganga Nagar', 'New Delhi']
                       vii.  ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
                       viii.  WZ-1,Old Ganga Nagar,Old Delhi
                       ix.  ('WZ-1', ',', 'New Ganga Nagar,New Delhi')
                       x.  ValueError: substring not found

                                                         PROGRAMMING PROBLEMS
                    1.  Write a program to input line(s) of text from the user until enter is pressed. Count the total number of characters in the text (including
                       white spaces), total number of alphabets, total number of digits, total number of special symbols and total number of words in the given
                       text. (Assume that each word is separated by one space).
                  Ans.   text = ""
                       while True:
                           line = input("Enter a line of text (press Enter to finish): ")
                           if not line:
                               break
                           text += line + "\n"

                       totalCharacters = len(text)
                       totalAlphabets = 0
                       totalDigits = 0
                       totalSpecialSymbols = 0

                       for char in text:
                           if char.isalpha():
                               totalAlphabets += 1
                           elif char.isdigit():
                               totalDigits += 1
                           else:
                               totalSpecialSymbols += 1

                       words = text.split()
                       totalWords = len(words)

                       print("\nText Statistics:")
                       print(f"Total Characters: {totalCharacters}")
                       print(f"Total Alphabets: {totalAlphabets}")
                       print(f"Total Digits: {totalDigits}")
                       print(f"Total Special Symbols: {totalSpecialSymbols}")
                       print(f"Total Words: {totalWords}")
                    2.  Write a user defined function to convert a string with more than one word into title case string where string is passed as parameter. (Title
                       case means that the first letter of each word is capitalised)
                  Ans.  def convertToTitleCase(inputString):
                           words = inputString.split()
                           titleCaseWords = []

                           for word in words:
                               titleCaseWords.append(word.capitalize())

                           titleCaseString = ' '.join(titleCaseWords)
                           return titleCaseString

                       inputString = input("Enter a string with more than one word: ")

                                                                                                            Strings   367
   376   377   378   379   380   381   382   383   384   385   386