Program to Count Number of Occurrence of Character in String

Program to Count Number of Occurrence of Character in String

  • Write a program to Count Number of Occurrence of Character in String in C
  • Write a program to Count Number of Occurrence of Character in String in C++
  • Write a program to Count Number of Occurrence of Character in String in Python
  • Write a program to Count Number of Occurrence of Character in String in PHP
  • Write a program to Count Number of Occurrence of Character in String in Java
  • Write a program to Count Number of Occurrence of Character in String in Java Script
  • Write a program to Count Number of Occurrence of Character in String in C#

Explanation:

Iterating through a string and counting the number of times a certain character appears is how you count the occurrences of a character in a string. The general reasoning is as follows:

Logic Steps:

  1. Initialize a Counter:
    • Start a counter variable at 0.
  2. Iterate Through the String:
    • Loop through each character in the string.
  3. Check for the Character:
    • Compare each character with the target character.
    • If they match, increment the counter.
  4. Output the Result:
    • After the loop, the counter holds the number of occurrences of the character.

Program to Count Number of Occurrence of Character in String

#include <stdio.h>

int main() {
    char str[100], ch;
    int count = 0;
    
    // Scan the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    
    // Scan the character to count
    printf("Enter the character to count: ");
    scanf("%c", &ch);
    
    // Count occurrences of character
    for(int i = 0; str[i] != '\0'; i++) {
        if(str[i] == ch) {
            count++;
        }
    }
    
    // Print the result
    printf("Character '%c' occurred %d times.\n", ch, count);
    
    return 0;
}

#include <iostream>
#include <string>

int main() {
    std::string str;
    char ch;
    int count = 0;
    
    // Scan the string
    std::cout << "Enter a string: ";
    std::getline(std::cin, str);
    
    // Scan the character to count
    std::cout << "Enter the character to count: ";
    std::cin >> ch;
    
    // Count occurrences of character
    for(char c : str) {
        if(c == ch) {
            count++;
        }
    }
    
    // Print the result
    std::cout << "Character '" << ch << "' occurred " << count << " times." << std::endl;
    
    return 0;
}

str = input("Enter a string: ")

# Scan the character to count
ch = input("Enter the character to count: ")

# Count occurrences of character
count = str.count(ch)

# Print the result
print(f"Character '{ch}' occurred {count} times.")

<?php
// Scan the string
echo "Enter a string: ";
$str = trim(fgets(STDIN));

// Scan the character to count
echo "Enter the character to count: ";
$ch = trim(fgets(STDIN));

// Count occurrences of character
$count = substr_count($str, $ch);

// Print the result
echo "Character '$ch' occurred $count times.\n";
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Initialize scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        // Scan the string
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();
        
        // Scan the character to count
        System.out.print("Enter the character to count: ");
        char ch = scanner.next().charAt(0);
        
        // Count occurrences of character
        int count = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == ch) {
                count++;
            }
        }
        
        // Print the result
        System.out.println("Character '" + ch + "' occurred " + count + " times.");
    }
}

let str = prompt("Enter a string: ");  // Use prompt for input in browsers

// Scan the character to count
let ch = prompt("Enter the character to count: ");

// Count occurrences of character
let count = 0;
for (let i = 0; i < str.length; i++) {
    if (str[i] === ch) {
        count++;
    }
}

// Print the result
console.log(`Character '${ch}' occurred ${count} times.`);

using System;

class Program {
    static void Main() {
        // Scan the string
        Console.Write("Enter a string: ");
        string str = Console.ReadLine();
        
        // Scan the character to count
        Console.Write("Enter the character to count: ");
        char ch = Console.ReadLine()[0];
        
        // Count occurrences of character
        int count = 0;
        foreach (char c in str) {
            if (c == ch) {
                count++;
            }
        }
        
        // Print the result
        Console.WriteLine($"Character '{ch}' occurred {count} times.");
    }
}

List of All Programs