Last index of a Character Python

PROGRAM TO FIND THE LAST INDEX OF A CHARACTER IN THE GIVEN STRING



# 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)


OUTPUT
Last index is 10

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java