Afternoon brain Watt, thought for a while did not come up with algorithms, just convulsions finally realized, hereby recorded.  ̄- ̄| |
Topic
(a) Create a function with the name FINDCHR (), FINDCHR () to find the character char in string strings, find the index that returns the value, otherwise return-1.
(b) Create another function called RFINDCHR (), looking for the character char last occurrence. It works like FINDCHR (), but it looks forward from the end of the string.
(c) Create a third function, called SUBCHR (), SUBCHR () similar to FINDCHR (), and the difference is that if a matching character is found, the original character is replaced with a new one. Returns the modified string.
Note: You cannot use the String.*find () or String.*index () functions and methods
Def findchr (String, char): if char in string: i=0 while String[i:i+len (char)]!=char: i+=1 Else: return i else: return-1 def rfindchr (String, char): if char in string: i=0 while i< (Len (String)-len (char) +1): if String[i:i+len (char)]!=char: i+=1 Else: s=i i+=1 Else: return s else: return-1def subchr (String, Origchar, Newchar): if Origchar in string: I =0 while String[i:i+len (Origchar)]!=origchar: i+=1 Else: new_string=string[:i]+newchar+ string[(I+len (Origchar)):] return new_string else: return-1
#运行结果print (FINDCHR (' xabcxxxx ', ' abc ') Print (RFINDCHR (' abcxxxxabc ', ' abc ')) Print (SUBCHR (' abcxxxx ', ' abc ', ' Lilip ')) 17lilipxxxx
"Python Core programming second Edition" after class exercise 6-12 answers