Given a string S and a character ‘c’, the task is to count the occurrence of the given character in the string.
Examples:
Input : S = “neveropen” and c = ‘e’
Output : 4
Explanation: ‘e’ appears four times in str.
Input : S = “abccdefgaa” and c = ‘a’
Output : 3
Explanation: ‘a’ appears three times in str.
Count occurrence of a given character in a string using simple Iteration:
Iterate through the string and during each iteration, check if the current character is equal to the given character c then increments the count variable which stores count of the occurrences of the given character c in the string.
Below is the implementation of above approach:
C++
#include <iostream>
#include <string>
using namespace std;
int count(string s, char c)
{
int res = 0;
for ( int i=0;i<s.length();i++)
if (s[i] == c)
res++;
return res;
}
int main()
{
string str= "neveropen" ;
char c = 'e' ;
cout << count(str, c) << endl;
return 0;
}
|
Java
class GFG
{
public static int count(String s, char c)
{
int res = 0 ;
for ( int i= 0 ; i<s.length(); i++)
{
if (s.charAt(i) == c)
res++;
}
return res;
}
public static void main(String args[])
{
String str= "neveropen" ;
char c = 'e' ;
System.out.println(count(str, c));
}
}
|
Python3
def count(s, c) :
res = 0
for i in range ( len (s)) :
if (s[i] = = c):
res = res + 1
return res
str = "neveropen"
c = 'e'
print (count( str , c))
|
C#
using System;
public class GFG {
public static int count( string s, char c)
{
int res = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == c)
res++;
}
return res;
}
public static void Main()
{
string str = "neveropen" ;
char c = 'e' ;
Console.WriteLine(count(str, c));
}
}
|
Javascript
<script>
function count(s, c)
{
let res = 0;
for (let i = 0; i < s.length; i++)
{
if (s.charAt(i) == c)
res++;
}
return res;
}
let str= "neveropen" ;
let c = 'e' ;
document.write(count(str, c));
</script>
|
PHP
<?php
function count ( $s , $c )
{
$res = 0;
for ( $i = 0; $i < strlen ( $s ); $i ++)
if ( $s [ $i ] == $c )
$res ++;
return $res ;
}
$str = "neveropen" ;
$c = 'e' ;
echo count ( $str , $c ) ;
return 0;
?>
|
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(1)
Count occurrence of a given character in a string using Inbuild Functions:
The idea is to use inbuild method in different programming languages which returns the count of occurences of a character in a string.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str = "neveropen" ;
char c = 'e' ;
cout << count(str.begin(), str.end(), c);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
String str = "neveropen" ;
char c = 'e' ;
System.out.println(Collections.frequency(
Arrays.asList(str.split( "" )),
String.valueOf(c)));
}
}
|
Python3
str = "neveropen"
c = 'e'
print ( len ( str .split(c)) - 1 );
|
C#
using System;
using System.Linq;
class Program {
static void Main( string [] args) {
string str = "neveropen" ;
char c = 'e' ;
Console.WriteLine(str.Count(x => x == c));
}
}
|
Javascript
let str = "neveropen" ;
let c = 'e' ;
console.log(str.split(c).length - 1);
|
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(1)
Count occurrence of a given character in a string using Recursion
Use a recursive approach to count the occurrences of a given character in a string. Checks if the character at the current index matches the target character, increments the count if it does, and then makes a recursive call to check the remaining part of the string. The process continues until the end of the string is reached, and the accumulated count would be the result.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countinString( char ch, int idx, string s)
{
if (idx == s.size())
return 0;
int count = 0;
if (s[idx] == ch)
count++;
count += countinString(ch,idx+1,s);
return count;
}
int main(){
string str = "neveropen" ;
char c = 'e' ;
cout<<(countinString(c,0, str));
}
|
Java
public class Main {
public static int countInString( char ch, int idx, String s) {
if (idx == s.length())
return 0 ;
int count = 0 ;
if (s.charAt(idx) == ch)
count++;
count += countInString(ch, idx + 1 , s);
return count;
}
public static void main(String[] args) {
String str = "neveropen" ;
char c = 'e' ;
System.out.println(countInString(c, 0 , str));
}
}
|
Python3
def count_in_string(ch, idx, s):
if idx = = len (s):
return 0
count = 0
if s[idx] = = ch:
count + = 1
count + = count_in_string(ch, idx + 1 , s)
return count
if __name__ = = "__main__" :
str = "neveropen"
c = 'e'
print (count_in_string(c, 0 , str ))
|
C#
using System;
class Program {
static int CountInString( char ch, int idx, string s) {
if (idx == s.Length)
return 0;
int count = 0;
if (s[idx] == ch)
count++;
count += CountInString(ch, idx + 1, s);
return count;
}
static void Main( string [] args) {
string str = "neveropen" ;
char c = 'e' ;
Console.WriteLine(CountInString(c, 0, str));
}
}
|
Javascript
function countInString(ch, idx, s) {
if (idx === s.length)
return 0;
let count = 0;
if (s[idx] === ch)
count++;
count += countInString(ch, idx + 1, s);
return count;
}
const str = "neveropen" ;
const c = 'e' ;
console.log(countInString(c, 0, str));
|
Time Complexity: O(len), where len is the size of the string given.
Auxiliary Space: O(len), where len is the size of the string given.
This article is contributed by Sahil Rajput. If you like neveropen and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!