Find the Nth occurrence of a character in the given String | Function to find the Nth occurrence of a character ; Loop to find the Nth occurrence of the character ; Driver Code
def findNthOccur ( string , ch , N ) : NEW_LINE INDENT occur = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == ch ) : NEW_LINE INDENT occur += 1 ; NEW_LINE DEDENT if ( occur == N ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " ; NEW_LINE ch = ' e ' ; NEW_LINE N = 2 ; NEW_LINE print ( findNthOccur ( string , ch , N ) ) ; NEW_LINE DEDENT