Post History
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...
Answer
#2: Post edited
- # [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
# [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;}