Skip to content
Related Articles

Related Articles

Improve Article

Find the Nth occurrence of a character in the given String

  • Last Updated : 23 Jul, 2021

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++




// 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 character
int findNthOccur(string str,
                 char ch, int N)
{
    int occur = 0;
 
    // Loop to find the Nth
    // occurence 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 Code
int main()
{
    string str = "geeks";
    char ch = 'e';
    int N = 2;
    cout << findNthOccur(str, ch, N);
}

Java




// Java implementation to find the
// Nth occurrence of a character
import java.util.*;
 
class GFG
{
 
// Function to find the
// Nth occurrence of a character
static int findNthOccur(String str,
                    char ch, int N)
{
    int occur = 0;
 
    // Loop to find the Nth
    // occurence 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 Code
public 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




# Python3 implementation to find the
# Nth occurrence of a character
 
# Function to find the
# Nth occurrence of a character
def findNthOccur(string , ch, N) :
    occur = 0;
 
    # Loop to find the Nth
    # occurence of the character
    for i in range(len(string)) :
        if (string[i] == ch) :
            occur += 1;
 
        if (occur == N) :
            return i;
     
    return -1;
 
# Driver Code
if __name__ == "__main__" :
 
    string = "geeks";
    ch = 'e';
    N = 2;
    print(findNthOccur(string, ch, N));
 
# This code is contributed by AnkitRai01

C#




// C# implementation to find the
// Nth occurrence of a character
using System;
 
class GFG
{
 
// Function to find the
// Nth occurrence of a character
static int findNthOccur(String str,
                    char ch, int N)
{
    int occur = 0;
 
    // Loop to find the Nth
    // occurence 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 Code
public 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

Javascript




<script>
// Javascript implementation to find the
// Nth occurrence of a character.
 
 
// Function to find the
// Nth occurrence of a character
function findNthOccur(str, ch, N)
{
    var occur = 0;
 
    // Loop to find the Nth
    // occurence 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>
Output: 
2

 

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :