Page 271 - Computer Science Class 11 Without Functions
P. 271

11.6 Case Study

            In this section, we will develop a module named stringFns.py that enables us to perform the following operations
            on strings:
            1.   Form  a  string  comprising  distinct  characters  in  a  string,  replacing  uppercase  characters  with  corresponding
                lowercase characters.
            2.  Compute the number of matching characters in a pair of strings.
            3.  Find whether a string is a substring of another string.

            4.  Construct a string by reversing the sequence of the characters in the original string.
            11.6.1 Distinct Characters in a String Ignoring the Case


            To form a string comprising the distinct characters in a string, let us develop a program that takes a string, say str1,
            as input from the user.
            ●  Since a string may comprise a mix of upper and lowercase characters, we first construct a string of lowercase
              characters (line 8).
            ●  Next, we initialize the string of distinct characters (distinctChars) to an empty string (line 9).

            ●  Next, for every character in the string, if the character is not included (not present) in the string distinctChars,
              it is concatenated to distinctChars (lines 11-13).
            ●  Finally, the program outputs a string (distinctChars) of distinct characters (line 15).

            Program 11.1 Count the distinct characters in a given string.

              01 '''
              02 Objective: To determine unique characters in the given string
              03 Input: str1 - string value
              04 Output: string of unique characters
              05 '''
              06 myStr = input('Enter a String:')
              07
              08 myStr = myStr.lower()
              09 distinctChars = ''
              10
              11 for ch in myStr:
              12     if ch not in distinctChars:
              13         distinctChars += ch
              14
              15 print(distinctChars)
            11.6.2 The Number of Common Characters in a Pair of Strings

            To compute the count of matching characters in a pair of strings,  let us develop a program that takes a pair of strings,
            str1 and str2, as input from the user.
            ●  Since the two strings may comprise a mix of upper and lowercase characters, we construct strings that comprise the
              same letters (in lowercase) of the English alphabet as the original strings (line 12).
            ●  Next, we initialize a list distinctChar to empty list (line 14).

            ●  The for loop (lines 15-17) scans the concatenation of strings str1 and str2.
            ●  For every character in str1 + str2, we check whether the char is already present in distinctChar. If the
              char is not present, we append the char in distinctChar (line 17).
            ●  Finally, we print the length of the distinctChar, that is the number of distinct characters between str1 and str2
              (line 19).



                                                                                                       Strings   269
   266   267   268   269   270   271   272   273   274   275   276