Find last index of a character in a string | C # code to find last index character x in given string . ; Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code
using System ; class GFG { static int findLastIndex ( string str , char x ) { for ( int i = str . Length - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } public static void Main ( ) { string str = " geeksforgeeks " ; char x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) Console . WriteLine ( " Character ▁ not ▁ found " ) ; else Console . WriteLine ( " Last ▁ index ▁ is ▁ " + index ) ; } }