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

titleCaseString = convertToTitleCase(inputString)
                    print("Title Case String:", titleCaseString)
                 3.  Write a function deleteChar() which takes two parameters one is a string and other is a character. The function should create a new string
                    after deleting all occurrences of the character from the string and return the new string.
               Ans.   def deleteChar(inputString, charToDelete):
                        newString = ""
                        for char in inputString:
                            if char != charToDelete:
                                newString += char
                        return newString

                    # Example usage:
                    originalString = "example string with some characters"
                    characterToDelete = 'e'
                    resultString = deleteChar(originalString, characterToDelete)
                    print(f"Original String: {originalString}")
                    print(f"String after deleting '{characterToDelete}': {resultString}")
                 4.  Input a string having some digits. Write a function to return the sum of digits present in this string.
               Ans.   def sumOfDigits(inputString):
                        digitSum = 0
                        for char in inputString:
                            if char.isdigit():
                                digitSum += int(char)
                        return digitSum

                    userInput = input("Enter a string with some digits: ")
                    result = sumOfDigits(userInput)
                    print(f"Sum of digits in the string: {result}")
                 5.  Write a function that takes a sentence as an input parameter where each word in the sentence is separated by a space. The function
                    should replace each blank with a hyphen and then return the modified sentence.
               Ans.   def replaceBlanksWithHyphens(inputSentence):
                        modifiedSentence = inputSentence.replace(" ", "-")
                        return modifiedSentence

                    inputSentence = input("Enter a sentence: ")
                    modifiedSentence = replaceBlanksWithHyphens(inputSentence)
                    print("Modified Sentence:", modifiedSentence)








                                                             Answers
                Multiple Choice Questions
                1. (c)     2. (d)      3. (b)     4. (a)      5. (d)     6. (d)     7. (a)      8. (b)     9. (d)
                10. (c)    11. (b)     12. (a)    13. (c)     14. (b)    15. (a)    16. (d)     17. (b)    18. (a)
                True or False
                1. (F)     2. (F)      3. (T)     4. (F)      5. (T)     6. (F)     7. (F)
                Fill in the blanks
                1. str                2. backslash(\)      3. index              4. len()              5. str()
                6. slice              7. s                 8. title()


               368   Touchpad Computer Science (Ver. 2.0)-XI
   377   378   379   380   381   382   383   384   385   386   387