Find the Nth occurrence of a character in the given String
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:
Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.
Input: str = “Geeks”, ch = ‘e’, N = 2
Output: 2Input: 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 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
// 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
# 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#
// 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 |
Javascript
<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

