#include <stdio.h>
void print_digit_count(char number[]){
int count[10]={0};
for(int i=0;number[i]!='\0';i++){
int digit =number[i]-'0';
count[digit]++;
}
for(int i=0;i<10;i++){
if(count[i]>0){
printf("%d %d\n",i,count[i]);
}
}
}
int main() {
scanf("%s",number);
print_digit_count(number);
return 0;
}
The code defines a function `print_digit_count` that counts how many times each digit (0-9) appears in a string of digits. It uses an array `count` to store the counts, then prints each digit that appears at least once along with its count. The `main` function reads a string and calls this function.
- In `main`, the variable `number` is used in `scanf` but it is not declared. You need to declare an array of characters (like `char number[100]`) before using it.
- The `scanf` format `%s` reads a string without spaces, but it does not limit the input length. Consider specifying a maximum width, such as `%99s`, to avoid buffer overflow if the input is too long.