mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-10-29 22:17:23 +01:00
feat(solutions): add is-palindrome/c/function
This commit is contained in:
parent
96ca014afd
commit
a6bd36b754
2
.gitignore
vendored
2
.gitignore
vendored
@ -10,6 +10,8 @@ build
|
||||
__pycache__
|
||||
obj
|
||||
bin
|
||||
*.out
|
||||
*.exe
|
||||
|
||||
# testing
|
||||
coverage
|
||||
|
@ -6,5 +6,5 @@ int main() {
|
||||
while (scanf("%s", &input) != EOF) {
|
||||
printf("Hello, %s!", input);
|
||||
}
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
3
challenges/is-palindrome/solutions/c/function/README.md
Normal file
3
challenges/is-palindrome/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# is-palindrome/c/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 28 September 2021.
|
19
challenges/is-palindrome/solutions/c/function/character.c
Normal file
19
challenges/is-palindrome/solutions/c/function/character.c
Normal file
@ -0,0 +1,19 @@
|
||||
#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';
|
||||
}
|
||||
|
||||
const char character_to_upper(const char character) {
|
||||
int a_ascii_code = (int)'a';
|
||||
int z_ascii_code = (int)'z';
|
||||
if (character >= a_ascii_code && character <= z_ascii_code) {
|
||||
return character - 32;
|
||||
}
|
||||
return character;
|
||||
}
|
21
challenges/is-palindrome/solutions/c/function/character.h
Normal file
21
challenges/is-palindrome/solutions/c/function/character.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef CHARACTER_H
|
||||
#define CHARACTER_H
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Converts the character to uppercase.
|
||||
*
|
||||
* @param character
|
||||
* @return const char
|
||||
*/
|
||||
const char character_to_upper(const char character);
|
||||
|
||||
#endif
|
16
challenges/is-palindrome/solutions/c/function/input.c
Normal file
16
challenges/is-palindrome/solutions/c/function/input.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include "input.h"
|
||||
|
||||
#include <stdio.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/is-palindrome/solutions/c/function/input.h
Normal file
11
challenges/is-palindrome/solutions/c/function/input.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
/**
|
||||
* @brief Read a line from stdin.
|
||||
*
|
||||
* @return char*
|
||||
*/
|
||||
char* input();
|
||||
|
||||
#endif
|
18
challenges/is-palindrome/solutions/c/function/solution.c
Normal file
18
challenges/is-palindrome/solutions/c/function/solution.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
#include "input.h"
|
||||
#include "string.h"
|
||||
|
||||
int main() {
|
||||
char* string = input();
|
||||
string = string_to_upper(string);
|
||||
string = string_replace(string, ' ', '\0');
|
||||
bool is_palindrome = string_is_palindrome(string);
|
||||
free(string);
|
||||
printf("%s\n", is_palindrome ? "true" : "false");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
47
challenges/is-palindrome/solutions/c/function/string.c
Normal file
47
challenges/is-palindrome/solutions/c/function/string.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "character.h"
|
||||
|
||||
char* string_to_upper(const char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
character_append(result, character_to_upper(string[index]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
char* string_reverse(const char* string) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = string_length - 1; index != -1; index--) {
|
||||
character_append(result, string[index]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool string_is_palindrome(const char* string) {
|
||||
char* string_reversed = string_reverse(string);
|
||||
bool is_palindrome = strcmp(string_reversed, string) == 0;
|
||||
free(string_reversed);
|
||||
return is_palindrome;
|
||||
}
|
||||
|
||||
char* string_replace(const char* string, char search_value,
|
||||
char replace_value) {
|
||||
size_t string_length = strlen(string);
|
||||
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||
for (size_t index = 0; index < string_length; index++) {
|
||||
bool is_search_value = search_value == string[index];
|
||||
if (is_search_value) {
|
||||
character_append(result, replace_value);
|
||||
} else {
|
||||
character_append(result, string[index]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
43
challenges/is-palindrome/solutions/c/function/string.h
Normal file
43
challenges/is-palindrome/solutions/c/function/string.h
Normal file
@ -0,0 +1,43 @@
|
||||
#ifndef STRING_H
|
||||
#define STRING_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief Converts all the alphabetic characters in a string to uppercase.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_to_upper(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Reverse the characters in an array.
|
||||
*
|
||||
* @param string
|
||||
* @return char*
|
||||
*/
|
||||
char* string_reverse(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Returns true if the string is a palindrome (a palindrome is a word,
|
||||
* number, phrase, or other sequence of characters which reads the same backward
|
||||
* as forward).
|
||||
*
|
||||
* @param string The string to check.
|
||||
* @return true if the string is a palindrome, false otherwise.
|
||||
*/
|
||||
bool string_is_palindrome(const char* string);
|
||||
|
||||
/**
|
||||
* @brief Replace all the occurrences of search_value into replace_value in
|
||||
* the string.
|
||||
*
|
||||
* @param string
|
||||
* @param search_value A character search value.
|
||||
* @param replace_value A character containing the text to replace for match.
|
||||
* @return char*
|
||||
*/
|
||||
char* string_replace(const char* string, char search_value, char replace_value);
|
||||
|
||||
#endif
|
@ -17,7 +17,7 @@ int main() {
|
||||
printf("%d\n", numbers[index]);
|
||||
}
|
||||
free(numbers);
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void bubble_sort(int numbers[], const int length) {
|
||||
|
@ -17,7 +17,7 @@ int main() {
|
||||
printf("%d\n", numbers[index]);
|
||||
}
|
||||
free(numbers);
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void insertion_sort(int numbers[], const int length) {
|
||||
|
@ -17,7 +17,7 @@ int main() {
|
||||
printf("%d\n", numbers[index]);
|
||||
}
|
||||
free(numbers);
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void merge(int numbers[], int left_index, int middle_index, int right_index) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM gcc:11.2.0
|
||||
COPY ./ ./
|
||||
RUN gcc solution.c --output=solution
|
||||
RUN gcc *.c --output=solution
|
||||
CMD ["./solution"]
|
||||
|
@ -6,5 +6,5 @@ int main() {
|
||||
while (scanf("%s", &input) != EOF) {
|
||||
printf("Hello, %s!", input);
|
||||
}
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user