Last index of a Character Python
PROGRAM TO FIND THE LAST INDEX OF A CHARACTER IN THE GIVEN STRING
OUTPUT
Last index is 10
# Returns last index of x if it is
# present. Else returns -1.
def
findLastIndex(
str
, x):
# Traverse from right
for
i
in
range
(
len
(
str
)
-
1
,
-
1
,
-
1
):
if
(
str
[i]
=
=
x):
return
i
return
-
1
# Driver code
str
=
"geeksforgeeks"
x
=
'e'
index
=
findLastIndex(
str
, x)
if
(index
=
=
-
1
):
print
(
"Character not found"
)
else
:
print
(
"Last index is "
, index)
Last index is 10
Comments
Post a Comment