Q. Write an algorithm and program to count occurrence of a given character in a string



String :- String is a sequence of characters terminated with a null character \0 .
For example: "letsfindcourse interview questions";

Algorithm

//Algorithm To Count Occurrence Of A Given Character In A String
START
Step 1 ->  Input string and character.
Step 2 ->  for i=0 to length of the string.
if str[i] == ch
Increase the count by 1. 
Loop End
step 3->   Display Count.
STOP


Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Count Occurrence Of A Given Character In A String.
    #include <stdio.h>
    int count_character(char str[], char c) 
    { 
    int count = 0; 
    for (int i=0;str[i];i++) 
    if (str[i] == c) 
    count++; 
    return count; 
    } 
    int main() 
    { 
    char c = 'e', str[100]="letsfindcourse"; 
    int count= count_character(str, c);
    printf("Occurrence of given Character %c in the string %s : %d",c,str,count); 
    return 0; 
    } 
    
    //C++ Program To Count Occurrence Of A Given Character In A String.
    #include <iostream>
    #include <string> 
    using namespace std; 
    int count_character(string str, char c) 
    { 
    int count = 0; 
    for (int i=0;i<str.length();i++) 
    if (str[i] == c) 
    count++; 
    return count; 
    } 
    int main() 
    { 
    string str= "letsfindcourse"; 
    char c = 'e'; 
    cout <<"Occurrence of given Character "<<c<<" in the string "<<str<<" : "<<count_character(str, c) << endl; 
    return 0; 
    } 
    //Java Program To Count Occurrence Of A Given Character In A String.
    public class LFC
    {
    public static int count_character(String str, char c) 
    { 
    int count = 0; 
    for (int i=0; i<str.length(); i++) 
    { 
    if (str.charAt(i) == c) 
    count++; 
    }  
    return count; 
    } 
    public static void main(String args[]) 
    { 
    String str= "letsfindcourse"; 
    char c = 'e'; 
    System.out.println("Occurrence of given Character"+c+" in the string "+str+" : "+count_character(str, c)); 
    } 
    } 
    
    //Python Program To Count Occurrence Of A Given Character In A String.
    def count_character(str1, c) : 
    count = 0
    for i in range(len(str1)) : 
    if (str1[i] == c): 
    count = count + 1
    return count 
    str1= "letsfindcourse"
    c = 'e'
    print("Occurrence of given Character",c," in the string ",str1," : ",count_character(str1, c)) 
    
    //C# Program To Count Occurrence Of A Given Character In A String.
    using System; 
    public class LFC { 
    public static int count_character(string str, char c) 
    { 
    int count = 0; 
    for (int i = 0; i < str.Length; i++) 
    { 
    if (str[i] == c) 
    count++; 
    }  
    return count; 
    } 
    public static void Main() 
    { 
    string str = "letsfindcourse"; 
    char ch = 'e'; 
    Console.WriteLine("Occurrence of given Character e in the string "+str+" : "+count_character(str, ch)); 
    } 
    } 
    //PHP Program To Count Occurrence Of A Given Character In A String.
    <?php
    function count_character($str, $c) 
    { 
    $count = 0; 
    for ($i = 0; $i < strlen($str); $i++) 
    if ($str[$i] == $c) 
    $count++; 
    return $count; 
    } 
    // Driver Code 
    $str= "letsfindcourse"; 
    $c = 'e'; 
    $count=count_character($str, $c);
    echo "Occurrence of given Character $c in the string $str : $count "; 
    return 0; 
    ?>
    //C Program To Count Occurrence Of A Given Character In A String.
    #include <stdio.h>
    int count_character(char str[], char c) 
    { 
    int count = 0; 
    for (int i=0;str[i];i++) 
    if (str[i] == c) 
    count++; 
    return count; 
    } 
    int main() 
    { 
    char c = 'e', str[100]="letsfindcourse"; 
    int count= count_character(str, c);
    printf("Occurrence of given Character %c in the string %s : %d",c,str,count); 
    return 0; 
    } 
    

    Output

    Occurrence of given Character e in the string letsfindcourse : 2 
    

    Recommended Programs

       Program to sort an array element in ascending order using Insertion Sort.
       Program to check whether the given string is pangram or not.