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
ifstatement checks ifdropsis a multiple of3. If so,"Pling"is concatenated toresultusing thestrcatfunction. - The second
ifstatement checks ifdropsis a multiple of5. If so,"Plang"is concatenated toresultusing thestrcatfunction. - The thrd
ifstatement checks ifdropsis a multiple of7. If so,"Plong"is concatenated toresultusing thestrcatfunction.
Finally, the [strlen function] checks if result is empty: if so, the [sprintf function] formats drops as string into result.
17th Jul 2024
·
Found it useful?