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

print(f"Number of 'm' occurrences: {mCount}")
                         elif choice == '3':

                             digitCount = countDigits(string)
                             print(f"Number of digits: {digitCount}")
                         elif choice == '4':
                             modifiedString = replaceSpaces(string)
                             print(f"String with spaces replaced by '*': {modifiedString}")

                         elif choice == '5':
                             lowercaseString = convertToLowercase(string)
                             print(f"String converted to lowercase: {lowercaseString}")
                         elif choice == '6':
                             titleCaseString = convertToTitleCase(string)

                             print(f"String converted to title case: {titleCaseString}")
                         elif choice == '7':
                             print("Exiting the program.")
                             break
                         else:

                             print("Invalid choice. Please select a valid option.")
                 Program 35: Write a program that accepts two strings and checks whether the second string is a substring (forms part)
                 of the first string. If yes, the program should display the starting index that marks the beginning of the second string;
                 otherwise, it should display 'Substring DOES NOT exist.'

                 Ans. def findSubstring(string1, string2):
                         '''
                          Objective : To check whether a string is substring of another and tell its
                           index
                         Input Parameter : string1,string2 - string
                         Return Value : index – numeric value, string

                         '''
                         index = string1.find(string2)
                         if index != -1:
                             return index
                         else:

                             return "Substring DOES NOT exist."

                     string1 = input("Enter the first string: ")
                     string2 = input("Enter the second string: ")
                     result = findSubstring(string1, string2)

                     print(result)






                                                                                                           Practical  513
   522   523   524   525   526   527   528   529   530   531   532