-1

You are given a string S. Count the number of occurrences of all the digits in the string S.

Input: First line contains string S.

Output: For each digit starting from 0 to 9, print the count of their occurrences in the string S. So, print 10 lines, each line containing 2 space separated integers. First integer i and second integer count of occurrence of i. See sample output for clarification.

Constraints:1≤|S|≤100

I tried doing it this way. But I'm getting errors.

error: array subscript is not an integer. and error: invalid operands to binary.

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    double a[100],i,k;
    int b[10]={0,1,2,3,4,5,6,7,8,9},sum[10];
    int j;
    gets(a);
    k=strlen(a);
    for(i=0; i<10; i++){
        sum[i]=0;
    }
    for(i=0; i<k; i++){
        a[i]=a/(pow(10,k-i-1));
        for(j=0; j<k; j++){
            if(i+j<10 && a[i]==a[i+j])
               sum[i]++;
            else
                break;
        }
    }
    printf("%d/n",b[i]);
    puts(sum[i]);
    return 0;
}

Please help.

OjasV
  • 1
  • 1
  • 2
    You should not use `gets`. Moreover passing an array of double into gets won't work – Rohan Kumar Sep 05 '17 at 05:55
  • 1
    `double a[100]` --> `char a[100+1];`, `double i, k` --> `int i, k;` or `size_t i, k;` – BLUEPIXY Sep 05 '17 at 05:57
  • `a/` : You can not apply `/` to the array itself. – BLUEPIXY Sep 05 '17 at 06:03
  • 2
    You want to get a C book. – n. m. could be an AI Sep 05 '17 at 06:03
  • This looks like a homework question. Anyway, here are some similar questions that have been answered before: 1. https://stackoverflow.com/questions/42058150/how-to-find-how-many-times-a-number-has-been-repeated-in-an-array 2. https://stackoverflow.com/questions/7349053/counting-the-number-of-times-a-character-occurs-in-a-string-in-c 3. https://stackoverflow.com/questions/20446042/count-number-of-occurrences-of-a-digit-within-a-string 4. https://stackoverflow.com/questions/26155225/find-how-many-times-a-character-appear-in-string-in-c 5. https://stackoverflow.com/questions/6630139/counting-digi – Zax Sep 05 '17 at 06:02

1 Answers1

3

You're doing some mistakes in your code. Like using gets/puts for array input output, these methods are for strings only(i.e char[]) . In order to find the occurrences of digits you can simply maintain an array and just keep on incrementing the position with the desired offset(which can be found by subtracting the character with '0' character. See below code :

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char a[100];
    int b[10]={0,1,2,3,4,5,6,7,8,9},sum[10];
    int j, i, k;
    fgets(a, 100, stdin);
    k=strlen(a);
    a[k-1] = '\0'; // trim the last newline
    k--;

    for(i=0; i<10; i++)
        sum[i]=0;
    for(i=0; i<k; i++){
        int offset = a[i] - '0';
        sum[offset]++;
    }
    for(int i = 0; i < 10; i++) 
        printf("%d : %d\n", i, sum[i]);
    return 0;
} 

Running this code gave the following output:

~/Documents/src : $ gcc testArrSub.c 
~/Documents/src : $ ./a.out 
12344567889005532
0 : 2
1 : 1
2 : 2
3 : 2
4 : 2
5 : 3
6 : 1
7 : 1
8 : 2
9 : 1
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40