mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add prefix-suffix/c/function
This commit is contained in:
parent
ab09295fca
commit
eb6e11ab5f
3
challenges/prefix-suffix/solutions/c/function/README.md
Normal file
3
challenges/prefix-suffix/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# prefix-suffix/c/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 2 December 2021.
|
10
challenges/prefix-suffix/solutions/c/function/character.c
Normal file
10
challenges/prefix-suffix/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/prefix-suffix/solutions/c/function/character.h
Normal file
13
challenges/prefix-suffix/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/prefix-suffix/solutions/c/function/input.c
Normal file
19
challenges/prefix-suffix/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/prefix-suffix/solutions/c/function/input.h
Normal file
11
challenges/prefix-suffix/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
|
18
challenges/prefix-suffix/solutions/c/function/solution.c
Normal file
18
challenges/prefix-suffix/solutions/c/function/solution.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "input.h"
|
||||
#include "string.h"
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
char* string2 = input();
|
||||
bool is_preffix = string_starts_with(string, string2);
|
||||
bool is_suffix = string_ends_with(string, string2);
|
||||
free(string);
|
||||
free(string2);
|
||||
printf("%s\n", is_preffix ? "true" : "false");
|
||||
printf("%s\n", is_suffix ? "true" : "false");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
38
challenges/prefix-suffix/solutions/c/function/string.c
Normal file
38
challenges/prefix-suffix/solutions/c/function/string.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
bool string_starts_with(char* string, char* prefix) {
|
||||
size_t string_length = strlen(string);
|
||||
size_t prefix_length = strlen(prefix);
|
||||
bool is_prefix = string_length >= prefix_length;
|
||||
for (size_t index = 0; index < prefix_length && index < string_length && is_prefix; index++) {
|
||||
if (prefix[index] != string[index]) {
|
||||
is_prefix = false;
|
||||
}
|
||||
}
|
||||
return is_prefix;
|
||||
}
|
||||
|
||||
bool string_ends_with(char* string, char* suffix) {
|
||||
size_t string_length = strlen(string);
|
||||
size_t suffix_length = strlen(suffix);
|
||||
int index_start_of_the_end = string_length - suffix_length;
|
||||
bool is_suffix = true;
|
||||
if (index_start_of_the_end < 0) {
|
||||
is_suffix = false;
|
||||
} else {
|
||||
size_t suffix_index = 0;
|
||||
for (size_t string_index = index_start_of_the_end; string_index < string_length && is_suffix; string_index++) {
|
||||
if (string[string_index] != suffix[suffix_index]) {
|
||||
is_suffix = false;
|
||||
}
|
||||
suffix_index++;
|
||||
}
|
||||
}
|
||||
return is_suffix;
|
||||
}
|
26
challenges/prefix-suffix/solutions/c/function/string.h
Normal file
26
challenges/prefix-suffix/solutions/c/function/string.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef __STRING__
|
||||
#define __STRING__
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief Returns true if the string is a prefix of another string.
|
||||
*
|
||||
* @param string
|
||||
* @param prefix
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool string_starts_with(char* string, char* prefix);
|
||||
|
||||
/**
|
||||
* @brief Returns true if the string is a suffix of another string.
|
||||
*
|
||||
* @param string
|
||||
* @param suffix
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool string_ends_with(char* string, char* suffix);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user