0

How to get digit frequency for numbers 0 to 9 from a string in c

Am supposed to come up with a function to count the frequency of the numbers 0 to 9 in a string submitted by a user. How do I iterate through the string while counting the number of times each digit has occurred, if a digit in this range is not found then am supposed to register 0 for that number and then print out the frequencies separated by spaces. Here is the method I tried so far and got stuck void mysolution(char * m){ //declare our array of integers int arr[10]; for(int i=0;i<=9;i++){ arr[i]=i; } //declare another integer array for freqs int freqs[10]; for(int i=0;i<10;i++){ freqs[i]=0; } for(int t=0;t<strlen(m);t++){ //how do i chck presence of number in string and increment its frequencies } } int main(){ //change s to be read from stdin char s[MAX_SIZE]; fgets(s,MAX_SIZE,stdin); //mysolution(s); }

17th Apr 2022, 9:33 AM
Timothy Njiru
Timothy Njiru - avatar
7 Answers
+ 1
You can use the isdigit() function to check whether a single character is a digit: https://en.cppreference.com/w/c/string/byte/isdigit You can then iterate over the string to check for each character if it is a digit, and if so, increment the appropriate counter in the `freqs` array. The ASCII code of the character can be used to obtain the correct index, as all digits are layed out continuously after each other, starting at 48 ('0') all the way to 57 ('9'): https://en.m.wikipedia.org/wiki/ASCII For example, this is what the loop could look like: for (char* it = m; *it != '\0'; ++it) { if (isdigit(*it)) { ++freqs[*it - '0']; } } I don't think you will need the `arr` array.
17th Apr 2022, 9:52 AM
Shadow
Shadow - avatar
+ 1
Hard to say like this, you might want to update your code in the description. If the tests use huge sets of data, the input buffer could be too small, or an integer as the counter might no be sufficient. My program(s) for reference: https://code.sololearn.com/cArQ3xphY327/?ref=app
17th Apr 2022, 11:19 AM
Shadow
Shadow - avatar
0
Lemme try it out, will update you as soon as I get a result.
17th Apr 2022, 10:07 AM
Timothy Njiru
Timothy Njiru - avatar
0
Seven out eleven test cases failed, what section could be making the other test cases fail?
17th Apr 2022, 10:17 AM
Timothy Njiru
Timothy Njiru - avatar
0
It is a Hacker Rank challenge, C programming language , an assignment called digit frequency
17th Apr 2022, 11:29 AM
Timothy Njiru
Timothy Njiru - avatar
0
Here is an example I have written: #define _POSIX_C_SOURCE 200809L #include <stdio.h> #include <stdlib.h> #include <string.h> void countdigits(const char *string, size_t *counter){ for(size_t i = 0; i < strlen(string); i++){ if(string[i] - '0' < 0 || string[i] - '0' > 9){ //counter[0]++; } else { counter[string[i] - '0']++; } } } int main(void){ char *gl_buffer; size_t gl_size = 0; size_t digits[10] = {0}; puts("Get user input: "); if(getline(&gl_buffer, &gl_size, stdin) == -1) perror("Getline failed"); countdigits(gl_buffer, digits); for(size_t i = 0; i < 10; i++){ printf("%zu has %zu occurances.\n", i, digits[i]); } exit(EXIT_SUCCESS); } You an comment the "//counter[0]++" if you need everything not a number to count as 0 as in your statement.
17th Apr 2022, 2:10 PM
William Owens
William Owens - avatar
0
Thanks William Owens, will try it out when am free.
18th Apr 2022, 2:54 PM
Timothy Njiru
Timothy Njiru - avatar