-2

I want to compare the integers in a string with integers (0-9) and I wrote this -

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

int main() {
  char num[100];
  int count = 0;
  scanf("%s", num);
  int len = strlen(num);
  for (int i = 0; i <= 9; i++)
  {
    for (int j = 0; j <= len; j++)
    {
      if (i == (num[j] - '0'))
      {
        count++;
      }
    }
    printf("%d ", count);
    count = 0;
  }
  return 0;
}

No problems with this (works in most cases but it is failing in few cases). So can you please give me alternate and best idea to do this? Thanks in advance

Complete pic - Pic of problem statement with working cases

Ravi Kumar
  • 11
  • 1
  • 7
  • 2
    ⟼Remember, it's always important, *especially* when learning and asking questions on Stack Overflow, to keep your code as organized as possible. [Consistent indentation](https://en.wikipedia.org/wiki/Indentation_style) helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on. – tadman May 21 '21 at 05:45
  • 3
    Which particular cases are failing? – bereal May 21 '21 at 05:45
  • What values does it fail on, and what happens in those cases? Have you tried to step it with a debugger to see what happens in those cases? (Do!) – Nate Eldredge May 21 '21 at 05:46
  • 2
    Note that a more efficient and generally better approach is `isdigit()` from ``. – Nate Eldredge May 21 '21 at 05:47
  • @bereal I don't know which cases are failing. I'm practicing this in hacker rank – Ravi Kumar May 21 '21 at 05:50
  • 2
    @RaviKumar one potential candidate is input that contains spaces. `scanf` will only read until the first space. – bereal May 21 '21 at 05:52
  • 1
    @DYZ: I don't think so. `num[len]` is the trailing null, which is a valid character but not between 0 and 9, so it will not be counted as a digit. It's a waste of time to check it, but not incorrect. – Nate Eldredge May 21 '21 at 05:52
  • @RaviKumar: Then at least please link to the precise specification of the problem. – Nate Eldredge May 21 '21 at 05:54
  • 1
    [Edit] and show some examples of input and expected output vs. actual output. Show some cases where it works and some cases where it doesn't work. – Jabberwocky May 21 '21 at 06:00
  • @bereal I tried gets but it says gets() is invalid in C99 – Ravi Kumar May 21 '21 at 06:01
  • 1
    @RaviKumar try [`fgets()`](https://stackoverflow.com/a/1694042/770830) – bereal May 21 '21 at 06:03
  • @bereal I don't know why but with fgets(), it fails in 7/11 test cases – Ravi Kumar May 21 '21 at 06:10
  • @Jabberwocky Yes, I did. Will you please have a look at it – Ravi Kumar May 21 '21 at 06:10
  • 2
    So, after reading the assignment, it's clear that your array is too short. Input is said to be up to 1000 chars. `scanf` has nothing to do with that, because the input format is guaranteed to be alphanumeric. – bereal May 21 '21 at 06:11
  • @bereal Attention to detail is great!! I missed it. Thanks for your help – Ravi Kumar May 21 '21 at 06:15
  • @RaviKumar next time don't post pictures of text, post text as text. – Jabberwocky May 21 '21 at 06:24
  • @Jabberwocky OK – Ravi Kumar May 21 '21 at 06:30

2 Answers2

1

The root cause is not in char comparison, but in the under-allocated buffer:

char num[100];

The assignment constraint is:

1 <= len(num) <= 1000

After increasing the buffer size, all the tests pass.

bereal
  • 32,519
  • 6
  • 58
  • 104
0

Besides a too small input buffer (i.e. 100 instead of 1001), I think your approach is too complex.

Instead of a nested loop, I'll suggest an array to count the frequency, i.e. an array with 10 elements so that you have a counter for each digit.

int main() {
  char num[1001];        // 1000 chars + 1 zero termination
  int count[10] = {0};   // Array of 10 zero initialized counters, one for each digit
  scanf("%1000s", num);  // At max accept 1000 chars input
  char* p = num;
  while (*p)
  {
      if (isdigit(*p) ++count[*p - '0'];
      ++p;
  }
  for (int i = 0; i < 10; ++i) printf("%d ", count[i]);
  puts("");
  return 0;
}

If you don't want to use isdigit you can instead do:

if (*p >= '0' && *p <= '9') ++count[*p - '0'];
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63