Find the Nth occurrence of a character in the given String | C ++ implementation to find the Nth occurrence of a character ; Function to find the Nth occurrence of a character ; Loop to find the Nth occurrence of the character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNthOccur ( string str , char ch , int N ) { int occur = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ch ) { occur += 1 ; } if ( occur == N ) return i ; } return -1 ; } int main ( ) { string str = " geeks " ; char ch = ' e ' ; int N = 2 ; cout << findNthOccur ( str , ch , N ) ; }