mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat(cli): add --all
option to run test
command
This commit is contained in:
10
challenges/hello-world/solutions/c/function/character.c
Normal file
10
challenges/hello-world/solutions/c/function/character.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include "character.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void character_append(char* string, char character) {
|
||||
size_t length = strlen(string);
|
||||
string[length] = character;
|
||||
string[length + 1] = '\0';
|
||||
}
|
13
challenges/hello-world/solutions/c/function/character.h
Normal file
13
challenges/hello-world/solutions/c/function/character.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef __CHARACTER__
|
||||
#define __CHARACTER__
|
||||
|
||||
/**
|
||||
* @brief Append a character to a string, assuming string points to an array
|
||||
* with enough space.
|
||||
*
|
||||
* @param string
|
||||
* @param character
|
||||
*/
|
||||
void character_append(char* string, char character);
|
||||
|
||||
#endif
|
19
challenges/hello-world/solutions/c/function/input.c
Normal file
19
challenges/hello-world/solutions/c/function/input.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* input() {
|
||||
char character;
|
||||
size_t length = 1;
|
||||
char* string = malloc(length * sizeof(char));
|
||||
*string = '\0';
|
||||
while ((character = getchar()) != '\n' && character != EOF) {
|
||||
length++;
|
||||
string = realloc(string, length * sizeof(char));
|
||||
character_append(string, character);
|
||||
}
|
||||
return string;
|
||||
}
|
11
challenges/hello-world/solutions/c/function/input.h
Normal file
11
challenges/hello-world/solutions/c/function/input.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __INPUT__
|
||||
#define __INPUT__
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
* @return char*
|
||||
*/
|
||||
char* input();
|
||||
|
||||
#endif
|
@ -1,10 +1,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "input.h"
|
||||
|
||||
int main() {
|
||||
char input[1024];
|
||||
while (scanf("%s", &input) != EOF) {
|
||||
printf("Hello, %s!", input);
|
||||
}
|
||||
char *string = input();
|
||||
printf("Hello, %s!\n", string);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user