# Count occurrences of a character in a repeated string

Given an integer N and a lowercase string. The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters.

Examples:

```Input : N = 10 str = "abcac"
Output : 4
Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first 10 letters 'a' occurs 4  times.

Input: N = 10, str = "aba"
Output : 7
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
1. Find the occurrences of character ‘a’ in the given string.
2. Find the No. of repetitions which are required to find the ‘a’ occurrences.
3. Multiply the single string occurrences to the No. of repetitions.
4. If given n is not the multiple of given string size then we will find the ‘a’ occurrences in the remaining substring.

Below is the implementation of above approach:

## C++

 `// CPP program to find the occurrences of ` `// character x in the infinite repeated string ` `// upto length n ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the character 'a' ` `int` `countChar(string str, ``char` `x) ` `{ ` `    ``int` `count = 0, n = 10; ` `    ``for` `(``int` `i = 0; i < str.size(); i++) ` `        ``if` `(str[i] == x) ` `            ``count++; ` ` `  `    ``// atleast k repetition are required ` `    ``int` `repititions = n / str.size(); ` `    ``count = count * repititions; ` ` `  `    ``// if n is not the multiple of the string size ` `    ``// check for the remaining repeating character. ` `    ``for` `(``int` `i = 0; i < n % str.size(); i++) { ` `        ``if` `(str[i] == x) ` `            ``count++; ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"abcac"``; ` `    ``cout << countChar(str, ``'a'``); ` `    ``return` `0; ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Java

 `// Java program to find the occurrences  ` `// of character x in the infinite  ` `// repeated string upto length n ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG ` `{ ` `// Function to count the character 'a' ` `static` `int` `countChar(String str, ``char` `x) ` `{ ` `    ``int` `count = ``0``; ` `    ``int` `n = ``10``; ` `    ``for` `(``int` `i = ``0``; i < str.length(); i++) ` `        ``if` `(str.charAt(i) == x) ` `            ``count++; ` ` `  `    ``// atleast k repetition are required ` `    ``int` `repititions = n / str.length(); ` `    ``count = count * repititions; ` ` `  `    ``// if n is not the multiple of the  ` `    ``// string size check for the remaining  ` `    ``// repeating character. ` `    ``for` `(``int` `i = ``0``; ` `            ``i < n % str.length(); i++)  ` `    ``{ ` `        ``if` `(str.charAt(i) == x) ` `            ``count++; ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``String str = ``"abcac"``; ` `    ``System.out.println(countChar(str, ``'a'``)); ` `} ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai `

## Python3

 `# Python3 program to find the occurrences of  ` `# character x in the infinite repeated string  ` `# upto length n  ` ` `  `# Function to count the character 'a'  ` `def` `countChar(``str``, x): ` `    ``count ``=` `0` `    ``for` `i ``in` `range``(``len``(``str``)):  ` `        ``if` `(``str``[i] ``=``=` `x) : ` `            ``count ``+``=` `1` `    ``n ``=` `10` `     `  `    ``# atleast k repetition are required  ` `    ``repititions ``=` `n ``/``/` `len``(``str``)  ` `    ``count ``=` `count ``*` `repititions  ` ` `  `    ``# if n is not the multiple of the  ` `    ``# string size check for the remaining  ` `    ``# repeating character. ` `    ``l ``=` `n ``%` `len``(``str``)  ` `    ``for` `i ``in` `range``(l): ` `        ``if` `(``str``[i] ``=``=` `x):  ` `            ``count ``+``=` `1` `    ``return` `count  ` ` `  `# Driver code  ` `str` `=` `"abcac"` `print``(countChar(``str``, ``'a'``)) ` ` `  `# This code is contributed  ` `# by sahishelangia `

## C#

 `// C# program to find the occurrences  ` `// of character x in the infinite  ` `// repeated string upto length n ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Function to count the character 'a' ` `static` `int` `countChar(``string` `str, ``char` `x) ` `{ ` `    ``int` `count = 0; ` `    ``int` `n = 10; ` `    ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``if` `(str[i] == x) ` `            ``count++; ` ` `  `    ``// atleast k repetition are required ` `    ``int` `repititions = n / str.Length; ` `    ``count = count * repititions; ` ` `  `    ``// if n is not the multiple of the  ` `    ``// string size check for the remaining  ` `    ``// repeating character. ` `    ``for` `(``int` `i = 0; ` `             ``i < n % str.Length; i++)  ` `    ``{ ` `        ``if` `(str[i] == x) ` `            ``count++; ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `str = ``"abcac"``; ` `    ``Console.WriteLine(countChar(str, ``'a'``)); ` `} ` `} ` ` `  `// This code is contributed ` `// by Akanksha Rai `

## PHP

 ` `

Output:

`4`

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

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 :

2

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