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

●  On execution of line 9, the two strings may comprise a mix of upper and lowercase characters, we first construct a
                   string of lowercase characters.
                 ●  Line 10, we find the length (lengthSubstr) of the substring.
                 ●  On execution of lines 11–13, The for-loop looks for a substring of length lengthSubstr that matches the given
                   substr.
                 ● Line 13, if substr is found, the function returns True and False otherwise at line 14.

                 Program 12.3 Searching for a substring in another string.

                   01 def isSubString(string, substr):
                   02     '''
                   03     Objective: To search for a substring in given string
                   04     Input Parameters:
                   05       substr - string value
                   06       string - string value
                   07     Return value: True- if substr is present in string, False, otherwise
                   08     '''
                   09     substr, string = substr.lower(), string.lower()
                   10     lengthSubstr = len(substr)
                   11     for index in range(len(string)):
                   12         if substr == string[index:index+lengthSubstr]:
                   13             return True
                   14     return False
                 12.4.4 Constructing the Reverse of a Given String

                 Let us develop a function reverseStr() that takes a string as an input parameter.
                 ● Line 8, we initialize the string comprising characters in reverse order (revString) to an empty string.
                 ● Line 9, we find the length of string (lengthSubstr).
                 ●  On execution of lines 10–11, we scan the string and append every subsequent letter to the beginning of the revString.
                 ● On execution of line 12, finally, the function returns the reversed string (revString).

                 Program 12.4 Constructing the reverse of a string.

                   01 def reverseStr(string):
                   02     '''
                   03     Objective: To reverse the given string
                   04     Input Parameters:
                   05       string - string value
                   06     Return value: reverseStr- reversed string
                   07     '''
                   08     revString = ''
                   09     lengthSubstr = len(string)
                   10     for char in string:
                   11         revString = char + revString
                   12     return revString
                 On executing the Python module stringFns.py and invoking the appropriate functions, Python responded as
                 follows:
                  >>> distinct('Computer Science')
                      'computer sin'
                  >>> findCommon('Shekhar', 'Shayna')
                      3
                  >>> isSubString('Python programming', 'program')
                      True
                  >>> reverseStr('Python')
                      'nohtyP'

                                                                                                            Strings   351
   360   361   362   363   364   365   366   367   368   369   370