Find the Nth occurrence of a character in the given String

08 Jun, 2022
Given string str, a character ch, and a value N, the task is to find the index of the Nth occurrence of the given character in the given string. Print -1 if no such occurrence exists.
Examples: 
Input: str = “Geeks”, ch = ‘e’, N = 2 Output: 2
Input: str = “GFG”, ch = ‘e’, N = 2 Output: -1  
Approach: 
Traverse the string character by character.
Check for each character if it matches with the given character.
Increment the count by 1, if it matches with the given character.
If the count becomes equal to N, return the latest found index
If the count does not match with N after the traversal, return -1
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to find the// Nth occurrence of a character #include <bits/stdc++.h>using namespace std; // Function to find the// Nth occurrence of a characterint findNthOccur(string str,                 char ch, int N){    int occur = 0;     // Loop to find the Nth    // occurrence of the character    for (int i = 0; i < str.length(); i++) {        if (str[i] == ch) {            occur += 1;        }        if (occur == N)            return i;    }    return -1;} // Driver Codeint main(){    string str = "geeks";    char ch = 'e';    int N = 2;    cout << findNthOccur(str, ch, N);}
// Java implementation to find the// Nth occurrence of a characterimport java.util.*; class GFG{ // Function to find the// Nth occurrence of a characterstatic int findNthOccur(String str,                    char ch, int N){    int occur = 0;     // Loop to find the Nth    // occurrence of the character    for (int i = 0; i < str.length(); i++)    {        if (str.charAt(i) == ch)        {            occur += 1;        }        if (occur == N)            return i;    }    return -1;} // Driver Codepublic static void main(String[] args){    String str = "geeks";    char ch = 'e';    int N = 2;    System.out.print(findNthOccur(str, ch, N));}} // This code is contributed by 29AjayKumar
# Python3 implementation to find the# Nth occurrence of a character # Function to find the# Nth occurrence of a characterdef findNthOccur(string , ch, N) :    occur = 0;     # Loop to find the Nth    # occurrence of the character    for i in range(len(string)) :        if (string[i] == ch) :            occur += 1;         if (occur == N) :            return i;         return -1; # Driver Codeif __name__ == "__main__" :     string = "geeks";    ch = 'e';    N = 2;    print(findNthOccur(string, ch, N)); # This code is contributed by AnkitRai01
// C# implementation to find the// Nth occurrence of a characterusing System; class GFG{ // Function to find the// Nth occurrence of a characterstatic int findNthOccur(String str,                    char ch, int N){    int occur = 0;     // Loop to find the Nth    // occurrence of the character    for (int i = 0; i < str.Length; i++)    {        if (str[i] == ch)        {            occur += 1;        }        if (occur == N)            return i;    }    return -1;} // Driver Codepublic static void Main(String[] args){    String str = "geeks";    char ch = 'e';    int N = 2;    Console.Write(findNthOccur(str, ch, N));}} // This code is contributed by PrinciRaj1992
<script>// Javascript implementation to find the// Nth occurrence of a character.  // Function to find the// Nth occurrence of a characterfunction findNthOccur(str, ch, N){    var occur = 0;     // Loop to find the Nth    // occurrence of the character    for (var i = 0; i < str.length; i++) {        if (str[i] == ch) {            occur += 1;        }        if (occur == N)            return i;    }    return -1;} var str = "geeks";    var ch = 'e';    var N = 2;    document.write( findNthOccur(str, ch, N)); // This code is contributed by SoumikMondal</script>
2
 
Time complexity: O(N) where N is length of string
Auxiliary Space: O(1)
29AjayKumar
princiraj1992
ankthon
SoumikMondal
abhishek0719kadiyan
sagar0719kumar
hasani
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.