We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C
- Arrays and Strings
- Digit Frequency
- Discussions
Digit Frequency
Digit Frequency
Sort by
recency
|
1138 Discussions
|
Please Login in order to post a comment
include
include
include
//if you dont know much about pointer int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int a[10]={0,0,0,0,0,0,0,0,0}; char s; s = (char)malloc(1000*sizeof(char)); scanf("%s", s); while(*s != '\0'){ if(*s=='0') ++a[0]; if(*s=='1') ++a[1]; if(*s=='2') ++a[2]; if(*s=='3') ++a[3]; if(*s=='4') ++a[4]; if(*s=='5') ++a[5]; if(*s=='6') ++a[6]; if(*s=='7') ++a[7]; if(*s=='8') ++a[8]; if(*s=='9') ++a[9]; ++s; } for(int i=0;i<10;i++){ printf("%d ",a[i]); } return 0; }
include
include
include
include
int main() {
}
the simpple logic is for (int i = 0; i < strlen(s); i++) { if (s[i] >= '0' && s[i] <= '9') { freq[s[i] - '0']++; } } here we can check the condition given and store it in a frequency array that we created previously