News for hackers. Stuff that matters.
Re:Programming Idioms by Stratoscope on Sunday August 15, 2021 @04:59PM (#28192491)
That is one of the most confusing pieces of C code I have seen lately. And it fails on an empty string: I would expect it to set b to 1 for an empty string, but it sets it to 0. Of course that could easily be fixed by setting b to 1 at the top.

Also, code like this should always be put inside a function that returns a value, not just written inline. Making it a function allows simpler and more understandable code too.

The funniest part is that it is not necessary to call strlen() at all! The whole thing can be written in a single pass over the string. Here is how I would code it in C:

  int OnlyDigits( char str[] ) {
      for( int i = 0;  str[i] != '\0';  ++i ) {
          if( str[i] < '0' || str[i] > '9' ) return 0;
      }
      return 1;
  }
Try it here:

https://replit.com/@geary/OnlyDigitsC

Re:Programming Idioms by pvtmert on Sunday August 15, 2021 @05:27PM (#28192681)
You do realize it returns 1 for an empty string right? I mean it doesn't have any digits in it...

What about adding a check of str[0] == 0 -> return 0 Also, giving char str[] will make it char* str. Which can be null. This may cause reading a random memory location (possibly segfault or use-after-free)

edit: I get the comments but empty string still contains no digits. Given the regex would be ^[0-9]+ (+ instead of *) What I want to say is string has a numerical value or not. Empty string is NaN.

Re:Programming Idioms by Stratoscope on Sunday August 15, 2021 @06:17PM (#28193050)
> You do realize it returns 1 for an empty string right? I mean it doesn't have any digits in it...

Yes, and that was deliberate on my part, as it meets my expectation of what such a function should do in this edge case.

The problem statement was "Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise."

To my mind, the question "is every character in the string a digit" should be equivalent to "are there any non-digits in the string" (with the answer inverted, of course).

Returning 0 (false) for the empty string makes those questions not equivalent. It makes the empty string a special case.

Of course the real problem is that the problem is under-specified. It should call out specifically what should happen for an empty string, because as illustrated here, this is something where reasonable people may disagree.

> Also giving char str[] will make it char* str. Which can be null. This may cause reading a random memory location (possibly segfault or use-after-free)

Well yes, of course. The point of my comment wasn't to write bullet-proof library-ready code, it was only to illustrate two things: code like this should always go in a function, and the entire task can be accomplished in a single pass through the string.

Thanks for keeping me on my toes!

Re:Programming Idioms by Someone on Sunday August 15, 2021 @05:40PM (#28192778)
“contains only digits” isn’t synonymous with “not (doesn’t have any digits)”

It is synonymous with “none of the characters is a non-digit”, which is true for the empty string.

Re:Programming Idioms by anonymoushn on Monday August 16, 2021 @08:35AM (#28197306)
Where did your choice of regex come from? The problem statement does not have any regex, and if you wanted to derive a regex from the problem statement you would probably need to get ^[0-9]*$ if you wanted to be correct.
Re:Programming Idioms by zeven7 on Sunday August 15, 2021 @05:38PM (#28192766)
Empty string contains no non-digits. Therefore it is only digits.