Find the Nth occurrence of a character in the given String

Given a string str, a character ch and a value N, the task is to find the index of 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 count does not matches with N after the traversal, return -1

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implemenation 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implemenation 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implemenation 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implemenation 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

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.