1
1
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:
Divlo 2021-12-02 15:25:38 +01:00
parent ab09295fca
commit eb6e11ab5f
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
8 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# prefix-suffix/c/function
Created by [@Divlo](https://github.com/Divlo) on 2 December 2021.

View 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';
}

View 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

View 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;
}

View File

@ -0,0 +1,11 @@
#ifndef __INPUT__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View 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;
}

View 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;
}

View 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