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)
{
if (drops % 3 == 0)
strcat(result, "Pling");
if (drops % 5 == 0)
strcat(result, "Plang");
if (drops % 7 == 0)
strcat(result, "Plong");
if (strlen(result) == 0)
sprintf(result, "%d", drops);
}
- The first
if
statement checks ifdrops
is a multiple of3
. If so,"Pling"
is concatenated toresult
using thestrcat
function. - The second
if
statement checks ifdrops
is a multiple of5
. If so,"Plang"
is concatenated toresult
using thestrcat
function. - The thrd
if
statement checks ifdrops
is a multiple of7
. If so,"Plong"
is concatenated toresult
using thestrcat
function.
Finally, the [strlen
function] checks if result
is empty: if so, the [sprintf
function] formats drops
as string into result
.
6th Nov 2024
·
Found it useful?