Skip to main content
added 92 characters in body
Source Link
jdt
  • 2.5k
  • 6
  • 22

I’ll start with the basics.

Your code returns an array of strings when it could have simply returned a string. A C-style string is an array that is terminated with a zero character.

You are coping data into fizzBuzz and then appending fizzBuzz into fbRange. You can write most of the data straight to fbRange.

Consider replacing fizzBuzzRange with something like this:

#define FBSIZE 15

char* fizzBuzzRange(size_t from, size_t to) {
    size_t length = to - from + 1;
    size_t arraySize = length * FBSIZE;
    char buf[FBSIZE];
    char* fbRange = (char*)malloc(arraySize);
    char* dst = fbRange;
    for (size_t index = from; index < to; index++) {
        bool isDivBy3 = index % 3 == 0;
        bool isDivBy5 = index % 5 == 0;

        if (isDivBy3 && isDivBy5) {
            memcpy(dst, "FizzBuzz", 8);
            dst += 8;
        }
        else if (isDivBy3) {
            memcpy(dst, "Fizz", 4);
            dst += 4;
        }
        else if (isDivBy5) {
            memcpy(dst, "Buzz", 4);
            dst += 4;
        }
        else {
            sprintf(buf, "%d", index);
            size_t len = strlen(buf);
            memcpy(dst, buf, len);
            dst += len;
        }
        *dst++ = '\n';
    }
    *dst = '\0';
    return fbRange;
}

int main() {
    char* fb = fizzBuzzRange(1, 100);
    printf("%s", fb);
}

There are still lots of optimizations to be made (see here) but that is where it starts to get complicated =)

I’ll start with the basics.

Your code returns an array of strings when it could have simply returned a string. A C-style string is an array that is terminated with a zero character.

You are coping data into fizzBuzz and then appending fizzBuzz into fbRange. You can write most of the data straight to fbRange.

Consider replacing fizzBuzzRange with something like this:

#define FBSIZE 15

char* fizzBuzzRange(size_t from, size_t to) {
    size_t length = to - from + 1;
    size_t arraySize = length * FBSIZE;
    char buf[FBSIZE];
    char* fbRange = (char*)malloc(arraySize);
    char* dst = fbRange;
    for (size_t index = from; index < to; index++) {
        bool isDivBy3 = index % 3 == 0;
        bool isDivBy5 = index % 5 == 0;

        if (isDivBy3 && isDivBy5) {
            memcpy(dst, "FizzBuzz", 8);
            dst += 8;
        }
        else if (isDivBy3) {
            memcpy(dst, "Fizz", 4);
            dst += 4;
        }
        else if (isDivBy5) {
            memcpy(dst, "Buzz", 4);
            dst += 4;
        }
        else {
            sprintf(buf, "%d", index);
            size_t len = strlen(buf);
            memcpy(dst, buf, len);
            dst += len;
        }
        *dst++ = '\n';
    }
    *dst = '\0';
    return fbRange;
}

int main() {
    char* fb = fizzBuzzRange(1, 100);
    printf("%s", fb);
}

There are still lots of optimizations to be made but that is where it starts to get complicated =)

I’ll start with the basics.

Your code returns an array of strings when it could have simply returned a string. A C-style string is an array that is terminated with a zero character.

You are coping data into fizzBuzz and then appending fizzBuzz into fbRange. You can write most of the data straight to fbRange.

Consider replacing fizzBuzzRange with something like this:

#define FBSIZE 15

char* fizzBuzzRange(size_t from, size_t to) {
    size_t length = to - from + 1;
    size_t arraySize = length * FBSIZE;
    char buf[FBSIZE];
    char* fbRange = (char*)malloc(arraySize);
    char* dst = fbRange;
    for (size_t index = from; index < to; index++) {
        bool isDivBy3 = index % 3 == 0;
        bool isDivBy5 = index % 5 == 0;

        if (isDivBy3 && isDivBy5) {
            memcpy(dst, "FizzBuzz", 8);
            dst += 8;
        }
        else if (isDivBy3) {
            memcpy(dst, "Fizz", 4);
            dst += 4;
        }
        else if (isDivBy5) {
            memcpy(dst, "Buzz", 4);
            dst += 4;
        }
        else {
            sprintf(buf, "%d", index);
            size_t len = strlen(buf);
            memcpy(dst, buf, len);
            dst += len;
        }
        *dst++ = '\n';
    }
    *dst = '\0';
    return fbRange;
}

int main() {
    char* fb = fizzBuzzRange(1, 100);
    printf("%s", fb);
}

There are still lots of optimizations to be made (see here) but that is where it starts to get complicated =)

Source Link
jdt
  • 2.5k
  • 6
  • 22

I’ll start with the basics.

Your code returns an array of strings when it could have simply returned a string. A C-style string is an array that is terminated with a zero character.

You are coping data into fizzBuzz and then appending fizzBuzz into fbRange. You can write most of the data straight to fbRange.

Consider replacing fizzBuzzRange with something like this:

#define FBSIZE 15

char* fizzBuzzRange(size_t from, size_t to) {
    size_t length = to - from + 1;
    size_t arraySize = length * FBSIZE;
    char buf[FBSIZE];
    char* fbRange = (char*)malloc(arraySize);
    char* dst = fbRange;
    for (size_t index = from; index < to; index++) {
        bool isDivBy3 = index % 3 == 0;
        bool isDivBy5 = index % 5 == 0;

        if (isDivBy3 && isDivBy5) {
            memcpy(dst, "FizzBuzz", 8);
            dst += 8;
        }
        else if (isDivBy3) {
            memcpy(dst, "Fizz", 4);
            dst += 4;
        }
        else if (isDivBy5) {
            memcpy(dst, "Buzz", 4);
            dst += 4;
        }
        else {
            sprintf(buf, "%d", index);
            size_t len = strlen(buf);
            memcpy(dst, buf, len);
            dst += len;
        }
        *dst++ = '\n';
    }
    *dst = '\0';
    return fbRange;
}

int main() {
    char* fb = fizzBuzzRange(1, 100);
    printf("%s", fb);
}

There are still lots of optimizations to be made but that is where it starts to get complicated =)