
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to find frequency of each digit in a string
Suppose we have a string s. The s contains letters and digits both. We shall have to find frequencies of each digit and display them. To do this we can make an array of size 10 for each digits (0 through 9), initially all elements are 0 inside the array, then when we encounter a digit simply increase the value of that index and finally print them all.
So, if the input is like s = "we85abc586wow236h69", then the output will be (Number 2, Freq 1) (Number 3, Freq 1) (Number 5, Freq 2) (Number 6, Freq 3) (Number 8, Freq 2) (Number 9, Freq 1)
To solve this, we will follow these steps −
Define an array freq of size: 10 and initialize all elements with 0
for initialize i := 0, when i < size of s, update (increase i by 1), do:
if s[i] is numeric, then:
increase freq[s[i] - ASCII of '0'] by 1
for initialize i := 0, when i < 10, update (increase i by 1), do:
if freq[i] > 0, then:
display (Number i , Freq freq[i])
Example
Let us see the following implementation to get better understanding −
#include <stdio.h> #include <string.h> void solve(char *s){ int freq[10] = {0}; for(int i = 0; i < strlen(s); i++){ if(s[i] >= '0' && s[i] <= '9'){ freq[s[i] - '0']++ ; } } for(int i = 0; i<10; i++){ if(freq[i] > 0) printf("(Number %d, Freq %d)
", i, freq[i]); } } int main(){ char *s = "we85abc586wow236h69"; solve(s); }
Input
"we85abc586wow236h69"
Output
(Number 2, Freq 1) (Number 3, Freq 1) (Number 5, Freq 2) (Number 6, Freq 3) (Number 8, Freq 2) (Number 9, Freq 1)
- Related Articles
- Find frequency of each word in a string in C#
- C++ Program to Find the Frequency of a Character in a String
- Find frequency of each word in a string in Java
- Find frequency of each word in a string in Python
- Find the frequency of a digit in a number using C++.
- Java Program to Find the Frequency of Character in a String
- Golang program to find the frequency of character in a string
- Check if frequency of each digit is less than the digit in Python
- Java program to find the Frequency of a character in a given String
- Write a C Program to count the frequency of each character
- Program to find second largest digit in a string using Python
- C++ program to find number in given range where each digit is distinct
- C++ program to find first digit in factorial of a number
- Write a Golang program to find the frequency of each element in an array
- Frequency of each character in String in Python
