exercism

Exercism solutions.
Log | Files | Refs

commit 73920fa56d924ffedde56f4dd7f1827989c54a8f
parent 36f046e6e9241b73ca1877f3b48a9e44d85da138
Author: dwrz <dwrz@dwrz.net>
Date:   Fri, 31 May 2019 22:08:13 +0000

Refactor c/beer-song/beer_song.c

Diffstat:
Mc/beer-song/src/beer_song.c | 102++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 52 insertions(+), 50 deletions(-)

diff --git a/c/beer-song/src/beer_song.c b/c/beer-song/src/beer_song.c @@ -1,57 +1,59 @@ #include <stdio.h> int verse(char* response, int bottles) { - if (bottles < 0) { - return 0; - } - - switch (bottles) { - case 0: - return sprintf(response, - "No more bottles of beer on the wall, " - "no more bottles of beer.\n" - "Go to the store and buy some more, " - "99 bottles of beer on the wall.\n"); - - case 1: - return sprintf(response, - "1 bottle of beer on the wall, " - "1 bottle of beer.\n" - "Take it down and pass it around, " - "no more bottles of beer on the wall.\n"); - - case 2: - return sprintf(response, - "2 bottles of beer on the wall, " - "2 bottles of beer.\n" - "Take one down and pass it around, " - "1 bottle of beer on the wall.\n"); - - default: - return sprintf(response, - "%d bottles of beer on the wall, " - "%d bottles of beer.\n" - "Take one down and pass it around, " - "%d bottles of beer on the wall.\n", - bottles, bottles, bottles - 1); - - } + if (response == NULL || bottles < 0) { + return 0; + } + + switch (bottles) { + case 0: + return sprintf(response, + "No more bottles of beer on the wall, " + "no more bottles of beer.\n" + "Go to the store and buy some more, " + "99 bottles of beer on the wall.\n"); + + case 1: + return sprintf(response, + "1 bottle of beer on the wall, " + "1 bottle of beer.\n" + "Take it down and pass it around, " + "no more bottles of beer on the wall.\n"); + + case 2: + return sprintf(response, + "2 bottles of beer on the wall, " + "2 bottles of beer.\n" + "Take one down and pass it around, " + "1 bottle of beer on the wall.\n"); + + default: + return sprintf(response, + "%d bottles of beer on the wall, " + "%d bottles of beer.\n" + "Take one down and pass it around, " + "%d bottles of beer on the wall.\n", + bottles, bottles, bottles - 1); + + } } void sing(char *response, int start, int finish) { - char *origin = response; - - for (int bottles = start; bottles >= finish; bottles--) { - int charsAdded = verse(response, bottles); - response += charsAdded; - - // Add a newline between verses, - // except for the last verse. - if (bottles != finish) { - sprintf(response, "\n"); - response += 1; - } - } - - response = origin; + if (response == NULL || start < 0 || finish < 0 || start <= finish) { + return; + } + + char *origin = response; + + for (int bottles = start; bottles >= finish; bottles--) { + int charsAdded = verse(origin, bottles); + origin += charsAdded; + + // Add a newline between verses, + // except for the last verse. + if (bottles != finish) { + sprintf(origin, "\n"); + origin += 1; + } + } }