6-12.
String.
(a) Create a function with the name FINDCHR (), which is declared below.
Def findchr (String, char)
FINDCHR () to look up the character char in string strings, find it to return the worthy index, otherwise return-1. You cannot use String.*find () or String.*index () functions and methods.
(b) Create another function called RFINDCHR () to find the last occurrence of the character char. It works like FINDCHR (), but it looks forward from the end of the string.
(c) Create a third function, called SUBCHR (), declared as follows.
Def subchr (String, Origchar, Newchar)
SUBCHR () is similar to FINDCHR (), but the difference is that if a matching character is found, the original character is replaced with a new character. Returns the modified string.
Answer
(a) The code is as follows:
Def findchr (String, char):
A = string
K = index =-1
For I in A:
K = k + 1
if i = = char:
index = k
Print Index
If index = = -1:print ' index = ', index
A = raw_input (' Please input a string ... ‘)
b = raw_input (' Please input a character to being find in this string ... ‘)
FINDCHR (A, B)
(b) The code is as follows:
Def rfindchr (String, char):
A = string
K = index =-1
For I in A:
K = k + 1
if i = = char:
index = k
Print Index
If index = = -1:print ' index = ', index
A = raw_input (' Please input a string ... ‘)
b = raw_input (' Please input a character to being find in this string ... ‘)
RFINDCHR (A, B)
(c) The code is as follows:
Def subchr (String, Origchar, Newchar):
Output = '
For I in Origchar:
If i = = string:
Output = output + Newchar
Else
Output = output + I
Print output
SUBCHR (' C ', ' abcddfasdfddacda ', ' K ')
The above originates from:
Http://www.cnblogs.com/balian/archive/2011/05/31/2064213.html
Python core Programming chapter sixth Exercise 6-12