Skip to content

Commit 2d1cfd8

Browse files
beer-song: update to latest spec (#676)
* beer-song: update to latest spec fixes #549 * [CI] Format C code Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 35ad079 commit 2d1cfd8

File tree

5 files changed

+446
-90
lines changed

5 files changed

+446
-90
lines changed
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
11
#include "beer_song.h"
2+
#include <stdint.h>
23
#include <stdio.h>
3-
#include <string.h>
44

5-
static unsigned int get_verse(unsigned int bottles, char *buffer)
5+
static uint16_t get_verse(uint8_t bottles, char **verse)
66
{
7-
unsigned int bytes_written = 0;
7+
uint16_t lines_written = 0;
88

99
if (bottles == 0) {
10-
bytes_written =
11-
sprintf(buffer,
12-
"No more bottles of beer on the wall, no more bottles of beer.\n"
13-
"Go to the store and buy some more, 99 bottles of beer on the wall.\n");
10+
sprintf(*verse,
11+
"No more bottles of beer on the wall, no more bottles of beer.");
12+
sprintf(*++verse,
13+
"Go to the store and buy some more, 99 bottles of beer on the wall.");
14+
lines_written = 2;
1415
} else if (bottles == 1) {
15-
bytes_written =
16-
sprintf(buffer,
17-
"1 bottle of beer on the wall, 1 bottle of beer.\n"
18-
"Take it down and pass it around, no more bottles of beer on the wall.\n");
19-
} else if (bottles == 2) {
20-
bytes_written =
21-
sprintf(buffer,
22-
"2 bottles of beer on the wall, 2 bottles of beer.\n"
23-
"Take one down and pass it around, 1 bottle of beer on the wall.\n");
24-
} else if (bottles <= 99) {
25-
bytes_written =
26-
sprintf(buffer,
27-
"%u bottles of beer on the wall, %u bottles of beer.\n"
28-
"Take one down and pass it around, %u bottles of beer on the wall.\n",
29-
bottles, bottles, bottles - 1);
16+
sprintf(*verse, "%u bottle of beer on the wall, %u bottle of beer.",
17+
bottles, bottles);
18+
sprintf(*++verse,
19+
"Take it down and pass it around, no more bottles of beer on the wall.");
20+
lines_written = 3;
21+
} else {
22+
sprintf(*verse, "%u bottles of beer on the wall, %u bottles of beer.",
23+
bottles, bottles);
24+
sprintf(*++verse,
25+
"Take one down and pass it around, %u bottle%sof beer on the wall.",
26+
bottles - 1, bottles - 1 == 1 ? " " : "s ");
27+
lines_written = 3;
3028
}
3129

32-
return bytes_written;
30+
return lines_written;
3331
}
3432

35-
void recite(unsigned int start_bottles, unsigned int take_down, char *buffer)
33+
void recite(uint8_t start_bottles, uint8_t take_down, char **song)
3634
{
37-
char *current_position = buffer;
38-
3935
while (take_down-- > 0) {
40-
current_position += get_verse(start_bottles--, current_position);
41-
strcpy(current_position, "\n");
42-
current_position++;
36+
song += get_verse(start_bottles--, song);
4337
}
44-
45-
strcpy(current_position - 1, "\0");
4638
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef BEER_SONG_H
22
#define BEER_SONG_H
33

4-
void recite(unsigned int start_bottles, unsigned int take_down, char *buffer);
4+
#include <stdint.h>
5+
6+
void recite(uint8_t start_bottles, uint8_t take_down, char **song);
57

68
#endif

exercises/practice/beer-song/.meta/tests.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ description = "last three verses"
2525

2626
[bc220626-126c-4e72-8df4-fddfc0c3e458]
2727
description = "all verses"
28-
include = false
29-
comment = "Requires reimplementation of output format, TBD see issue #549"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef BEER_SONG_H
22
#define BEER_SONG_H
33

4-
void recite(unsigned int start_bottles, unsigned int take_down, char *buffer);
4+
#include <stdint.h>
5+
6+
void recite(uint8_t start_bottles, uint8_t take_down, char **song);
57

68
#endif

0 commit comments

Comments
 (0)