Skip to content

Instantly share code, notes, and snippets.

@g-berthiaume
Last active January 28, 2021 16:50
Show Gist options
  • Save g-berthiaume/0d3a03646e91f82aca6c465b76a10a2b to your computer and use it in GitHub Desktop.
Save g-berthiaume/0d3a03646e91f82aca6c465b76a10a2b to your computer and use it in GitHub Desktop.
Weirdly, this seems to be a classic.
// run it online:
// https://repl.it/repls/SquigglyPessimisticCron
// gberthiaume 2020
//
#include <stdio.h>
#include <string.h>
#include <stdint.h>
char* fizzbuzz(uint8_t const n)
{
static char response[sizeof "fizzbuzz"] = {0};
char const* fizz = "";
char const* buzz = "";
if (n % 3 == 0) {
fizz = "fizz";
}
if (n % 5 == 0) {
buzz = "buzz";
}
if(*buzz != '\0' || *fizz != '\0' ) {
snprintf(response, sizeof("fizzbuzz"),"%s%s", fizz, buzz);
} else {
snprintf(response, sizeof("255"),"%d", n);
}
return response;
}
// Let's try it.
int main(void) {
for(size_t i = 1; i<=100; i++){
printf("%d -> %s\n", i, fizzbuzz(i));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment