Find last index of a character in a string | Java program to find last index of character x in given string . ; Returns last index of x if it is present Else returns - 1. ; Driver code ; String in which char is to be found ; char whose index is to be found
import java . io . * ; class GFG { static int findLastIndex ( String str , Character x ) { int index = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) == x ) index = i ; return index ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; Character x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) System . out . println ( " Character ▁ not ▁ found " ) ; else System . out . println ( " Last ▁ index ▁ is ▁ " + index ) ; } }