-1

Please tell me whats wrong in code the output is 00000000.
I know there is some mistake but cant find it.

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

int main()
{
     int c=0;
     char s[100];
     fgets(s, 100, stdin);
     printf("%s", s);
     for(int i=0;i<=9;i++)
     {
         for(int j=0;j<strlen(s);j++)
         {
             if(s[j]==i){
                 c++;
            }           
         }
         printf("%d", c);
     }

    return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
KalVik
  • 19
  • 1
  • 3

4 Answers4

1

Firstly, you are comparing a character with a int. Have a look at Char to int conversion in C for a solution.

Then, I would remember you that "0" is index 48 in ASCII table, not 0 : http://www.asciitable.com/

Artheriom
  • 11
  • 1
  • 3
  • I wouldn't bother to use the value `48`. All real-world character sets (including those the subject of standards) have the digits in a contiguous set starting with `0` and increasing. So subtracting `'0'` from any digit extracts the numerical equivalent (`'0' - '0'` - > `0`, `'1' - '0'` -> `1`, .... `'9' - '0'` -> `9`). – Peter Jun 22 '19 at 18:28
  • This is also a requirement in the C standard for compliant execution character sets. – Tom Blodget Jun 24 '19 at 02:52
0

The key problem is s[j]==i. That compares a char of the string to the values 0 to 9 ratter than to char '0' to '9'.

Another is the c is not reset to zero each loop.


Instead of looping 10 times, test if the char is a digit.

Instead of calling j<strlen(s) repeatedly, just test if s[j] == 0

 size_t digit_frequency[10] = {0};

 for (size_t i=0; s[i]; i++) {
   if (isdigit((unsigned char) s[i])) {
   // or  if (s[i] >= '0' && s[i] <= '9') {
     digit_frequency[s[i] - '0']++;
   }
 }

 for (size_t i=0; i<10; i++) {
   pritnf("%zu\n", s[i]);
 }

Code uses size_t rather than int as a string's length is limited to size_t - which may exceed int in extreme cases. Either work OK work size 100.

isdigit() declared in <ctype.h>

(unsigned char) used as isdigit() expect a value in the (unsigned char) and EOF and a char may be negative.

Various style choices - all function the same.

for (size_t i=0; s[i]; i++) {
for (size_t i=0; s[i] != '\0'; i++) {
for (size_t i=0; s[i] != 0; i++) {

"Given a string consisting of alphabets and digits" is a minor contraction. In C, a string includes the final null character: "A string is a contiguous sequence of characters terminated by and including the first null character" C11 §7.1.1 1. Yet folks often speak colloquially as is the null character was not part of the string.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Another style choice for the loop: `for (char *t = s; *t; ++t)`. Then use `*t` in the loop body in places where you use `s[i]`. – Peter Jun 22 '19 at 18:24
0

you did wrong in this line "if(s[j]==i)" and you didn't create any separate array for storing for frequency of digits and if you are using a single variable for frequency counting then it will show the cumulative of strings frequency

you can take is code as a reference

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

int main()
{
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%s", s);
    s = realloc(s, strlen(s) + 1);
    int len = strlen(s), i;
    int arr[10];
    for(i = 0; i < 10; i++)
        arr[i] = 0;
    for(i = 0; i < len; i++) {
        if(s[i] >= '0' && s[i] <= '9') {
            arr[(int)(s[i] - '0')]++;
        }
    }
    for(i = 0; i < 10; i++)
        printf("%d ", arr[i]);
    printf("\n");
    free(s);
    return 0;
}
Dhanu
  • 1
0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_LEN 1000

int main() {
    char s[MAX_LEN];
    char num[10]={'0','1','2','3','4','5','6','7','8','9'};
    int arr[10] = {0};
    scanf("%s",s);
    for (int i =0; i<strlen(s);i++)
    {
        for(int j=0;j<=9;j++)
        {
            if (s[i] == num[j])
            {
                arr[j]+=1;
            }

        }
    }
    for (int j =0;j<=9;j++)
    {
        printf("%d ",arr[j]);       
    }
        
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '23 at 20:35