Given a string, , consisting of alphabets and digits, find the frequency of each digit in the given string.
Input Format
The first line contains a string, which is the given number.
Constraints
All the elements of num are made of english alphabets and digits.
Output Format
Print ten space-separated integers in a single line denoting the frequency of each digit from to .
Sample Input 0
a11472o5t6
Sample Output 0
0 2 1 0 1 1 1 1 0 0
In the given string:
- occurs two times.
- and occur one time each.
- The remaining digits and don't occur at all.
Sample Input 1
lw4n88j12n1
Sample Output 1
0 2 1 0 1 0 0 0 2 0
Sample Input 2
1v88886l256338ar0ekk
Sample Output 2
1 1 1 2 0 1 2 0 5 0
HackerRank Digit Frequency Solution in C (Sample-1)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[1000];
int freq[10] = {0}; // Frequency of digits 0-9
// Read the input string
scanf("%s", s);
// Iterate through each character
for (int i = 0; s[i] != '\0'; i++) {
if (isdigit(s[i])) {
freq[s[i] - '0']++;
}
}
// Print frequencies of digits from 0 to 9
for (int i = 0; i < 10; i++) {
printf("%d ", freq[i]);
}
return 0;
}
HackerRank Digit Frequency Solution in C (Sample-2)
#include <stdio.h>
int main() {
char s[1000];
int freq[10] = {0};
scanf("%s", s);
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] >= '0' && s[i] <= '9') {
freq[s[i] - '0']++;
}
}
for (int i = 0; i < 10; i++) {
printf("%d ", freq[i]);
}
return 0;
}
HackerRank Digit Frequency Solution in C (Sample-3)
#include <stdio.h>
int main() {
char s[1000];
int freq[10] = {0};
scanf("%s", s);
for (int i = 0; s[i] != '\0'; i++) {
int ascii = (int)s[i];
if (ascii >= 48 && ascii <= 57) { // ASCII for '0' to '9'
freq[ascii - 48]++;
}
}
for (int i = 0; i < 10; i++) {
printf("%d ", freq[i]);
}
return 0;
}
0 Comments