sprintf
Function
raindrops.h
#ifndef RAINDROPS_H
#define RAINDROPS_H
void convert(char result[], int drops);
#endif
raindrops.c
#include "raindrops.h"
#include <stdio.h>
#include <string.h>
void convert(char result[], int drops)
{
sprintf(result, "%s%s%s", drops % 3 == 0 ? "Pling" : "",
drops % 5 == 0 ? "Plang" : "", drops % 7 == 0 ? "Plong" : "");
if (strlen(result) == 0)
sprintf(result, "%d", drops);
}
This approach allows for very concise, if not a bit obfuscated, code.
A series of ternary conditional operators check if drops
is a multiple of 3
, 5
, or 7
respectively.
If it is, the expression returns the appropriate raindrop sound (either "Pling"
, "Plang"
or "Plong"
); otherwise, the result is the empty string (""
).
Then, in the same statement, a single call to the sprintf
function concatenates all the above three strings.
Finally, the strlen
function checks if result
is empty: if so, sprintf
is used again to format drops
as string into result
.
6th Nov 2024
·
Found it useful?