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

if result == 1:
                       print("Input string is a palindrome.")

                   else:
                       print("Input string is not a palindrome.")
               Program 13: Write a program that takes a user-provided string as input and then generates a new string where the
               case (uppercase to lowercase and vice versa) of each character in the original input is reversed, and finally, print the
               resulting string.

              Ans. def swapCase(string):
                       '''
                       Objective :  To convert the case of the characters of the string

                       Input Parameter : string
                       Return Value : newString – string
                       '''
                       newString = ''
                       for char in string:

                           if char.isalpha():
                               if char.islower():
                                   newString += char.upper()
                               else:
                                   newString += char.lower()

                           else:
                               newString += char
                       return newString

                   string = input("Enter a string: ")

                   newString = swapCase(string)
                   print("The string after reversing the Case is : ", newString)
               Program 14: Write a program that takes a user-provided list as input and then generates a new list comprising swapped
               values at odd indices with those at even indices.

              Ans. def swappOddEvenIndex(myList):

                       '''
                       Objective : To swap values at odd indices with those at even indices
                       Input Parameter : myList - list
                       Return Value : myList - list with swapped values
                       '''
                       for x in range(0,len(myList)-1,2):

                           myList[x], myList[x+1] = myList[x+1],myList[x]
                       return myList

                   myList = []

               496   Touchpad Computer Science (Ver. 2.0)-XI
   505   506   507   508   509   510   511   512   513   514   515