Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges How many odd digits?

C (gcc), 41 bytes r;o(char*s){for(;*s;r+=*s++&1);return r;} Try it online! This is under the assumption that in a function solution, input has to be passed as parameter and output throug...

posted 1mo ago by Lundin‭  ·  edited 1mo ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2024-10-08T11:59:15Z (about 1 month ago)
  • # [C (gcc)], 41 bytes
  • <!-- language-all: lang-c -->
  • r;o(char*s){for(;*s;r+=*s++&1);return r;}
  • [Try it online!][TIO-m20dm1a0]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-m20dm1a0]: https://tio.run/##XYtBDoIwFET3PcUPRNMCGkREserOE@jSDWmLNNHW/IIb4tkRY0jQWc2blxGzqxBdh9xSURUYONaWFikPHMdwH7gwnC4YR1U3aAD5q/O1EbdGKti5Wmo7rw7El6rURsH5eDpTxwD3cfRAbeqSOvC2MJEX40Vge8c4Ib2Ae6EN0KfVkpGWwPfqeb0eejyGxQ8kyz9MV9l6k4/HJM3GmA/5jK/uDQ "C (gcc) – Try It Online"
  • This is under the assumption that in a function solution, input has to be passed as parameter and output through the return value. The result variable is allocated outside the function to get gcc implicit int declaration and zero initialization for free.
  • It's very straight-forward: input as string, iterate until null terminator found. Mask each character's ASCII value with 1 to see if odd or even, under the assumption that `'0'` == 48, an even value. From there, ASCII character are guaranteed to be adjacent in the symbol table.
  • I also tried to write it with recursion but got exactly the same amount of characters (41):
  • r;o(char*s){r+=*s&1;*s&&o(s+1);return r;}
  • # [C (gcc)], 41 bytes
  • <!-- language-all: lang-c -->
  • r;o(char*s){for(;*s;r+=*s++&1);return r;}
  • [Try it online!][TIO-m20dm1a0]
  • [C (gcc)]: https://gcc.gnu.org/
  • [TIO-m20dm1a0]: https://tio.run/##XYtBDoIwFET3PcUPRNMCGkREserOE@jSDWmLNNHW/IIb4tkRY0jQWc2blxGzqxBdh9xSURUYONaWFikPHMdwH7gwnC4YR1U3aAD5q/O1EbdGKti5Wmo7rw7El6rURsH5eDpTxwD3cfRAbeqSOvC2MJEX40Vge8c4Ib2Ae6EN0KfVkpGWwPfqeb0eejyGxQ8kyz9MV9l6k4/HJM3GmA/5jK/uDQ "C (gcc) – Try It Online"
  • This is under the assumption that in a function solution, input has to be passed as parameter and output through the return value. The result variable is allocated outside the function to get gcc implicit int declaration and zero initialization for free.
  • It's very straight-forward: input as string, iterate until null terminator found. Mask each character's ASCII value with 1 to see if odd or even, under the assumption that `'0'` == 48, an even value. From there, ASCII character are guaranteed to be adjacent in the symbol table.
  • I also tried to write it with recursion but got exactly the same amount of characters (41):
  • r;o(char*s){r+=*s&1;*s&&o(s+1);return r;}
  • Printing the result inside the function also works but is longer:
  • r=48;o(char*s){for(;*s;r+=*s++&1);puts(&r);}
#1: Initial revision by user avatar Lundin‭ · 2024-10-08T11:54:08Z (about 1 month ago)
# [C (gcc)], 41 bytes

<!-- language-all: lang-c -->

    r;o(char*s){for(;*s;r+=*s++&1);return r;}

[Try it online!][TIO-m20dm1a0]

[C (gcc)]: https://gcc.gnu.org/
[TIO-m20dm1a0]: https://tio.run/##XYtBDoIwFET3PcUPRNMCGkREserOE@jSDWmLNNHW/IIb4tkRY0jQWc2blxGzqxBdh9xSURUYONaWFikPHMdwH7gwnC4YR1U3aAD5q/O1EbdGKti5Wmo7rw7El6rURsH5eDpTxwD3cfRAbeqSOvC2MJEX40Vge8c4Ib2Ae6EN0KfVkpGWwPfqeb0eejyGxQ8kyz9MV9l6k4/HJM3GmA/5jK/uDQ "C (gcc) – Try It Online"

This is under the assumption that in a function solution, input has to be passed as parameter and output through the return value. The result variable is allocated outside the function to get gcc implicit int declaration and zero initialization for free.

It's very straight-forward: input as string, iterate until null terminator found. Mask each character's ASCII value with 1 to see if odd or even, under the assumption that `'0'` == 48, an even value. From there, ASCII character are guaranteed to be adjacent in the symbol table.

I also tried to write it with recursion but got exactly the same amount of characters (41):

    r;o(char*s){r+=*s&1;*s&&o(s+1);return r;}