1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

fix(solutions): memory issues thanks to -fsanitize=address flag with gcc

This commit is contained in:
Théo LUDWIG 2023-08-10 11:13:06 +02:00
parent 3b6cc97bb5
commit 0245c7a12c
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
96 changed files with 712 additions and 453 deletions

View File

@ -1,11 +1,16 @@
#include "character.h" #include "character.h"
#include <stdlib.h> void character_append(char* string, const char character) {
#include <string.h> size_t length = string_get_length(string);
character_append_at(string, character, length);
}
void character_append(char* string, char character) { void character_append_at(char* string, const char character, const size_t index) {
size_t length = strlen(string); size_t length = string_get_length(string);
string[length] = character; for (size_t index_string = length; index_string > index; index_string--) {
string[index_string] = string[index_string - 1];
}
string[index] = character;
string[length + 1] = '\0'; string[length + 1] = '\0';
} }

View File

@ -1,6 +1,11 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.
@ -10,6 +15,15 @@
*/ */
void character_append(char* string, char character); void character_append(char* string, char character);
/**
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
*
* @param string
* @param character
* @param index
*/
void character_append_at(char* string, const char character, const size_t index);
/** /**
* @brief Converts the character to uppercase. * @brief Converts the character to uppercase.
* *

View File

@ -7,10 +7,11 @@
int main() { int main() {
char* string = input(); char* string = input();
string = string_replace(string, '"', '\0'); string_remove_character(string, '"');
string = string_to_upper(string); string_to_uppercase(string);
string = string_acronym(string); char* result = string_acronym(string);
printf("%s\n", string); printf("%s\n", result);
free(string); free(string);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,47 +1,55 @@
#include "string.h" #include "string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h" #include "character.h"
char* string_to_upper(const char* string) { size_t string_get_length(const char* string) {
size_t string_length = strlen(string); size_t length = 0;
char* result = malloc(sizeof(char) * (string_length + 1)); while (string[length] != '\0') {
for (size_t index = 0; index < string_length; index++) { length++;
character_append(result, character_to_upper(string[index]));
} }
return result; return length;
} }
char* string_replace(const char* string, char search, char replace) { void string_to_uppercase(char* string) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1));
for (size_t index = 0; index < string_length; index++) { for (size_t index = 0; index < string_length; index++) {
bool is_search_value = search == string[index]; string[index] = character_to_upper(string[index]);
if (is_search_value) { }
character_append(result, replace); string[string_length] = '\0';
} else { }
character_append(result, string[index]);
void string_remove_character(char* string, char search) {
size_t string_length = string_get_length(string);
for (size_t index = 0; index < string_length; index++) {
if (string[index] == search) {
for (size_t index_string = index; index_string < string_length; index_string++) {
string[index_string] = string[index_string + 1];
}
string_length--;
index--;
} }
} }
return result; string[string_length] = '\0';
} }
char* string_acronym(char* string) { char* string_acronym(char* string) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1)); char* result = malloc(sizeof(char) * (string_length + 1));
char* current = malloc(sizeof(char) * (string_length + 1)); char current = '\0';
for (size_t index = 0; index < string_length; index++) { size_t result_index = 0;
if (string[index] == ' ') { bool is_first_character = true;
character_append(result, current[0]); for (size_t string_index = 0; string_index < string_length; string_index++) {
memset(current, 0, sizeof(char) * (string_length + 1)); if (string[string_index] == ' ') {
} else { result[result_index] = current;
character_append(current, string[index]); result_index += 1;
is_first_character = true;
} else if (is_first_character) {
current = string[string_index];
is_first_character = false;
} }
} }
character_append(result, current[0]); result[result_index] = current;
free(current); result_index += 1;
result[result_index] = '\0';
return result; return result;
} }

View File

@ -1,24 +1,37 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief Return the length of a string (excluding '\0').
*
* @param string
*/
size_t string_get_length(const char* string);
/** /**
* @brief Converts all the alphabetic characters in a string to uppercase. * @brief Converts all the alphabetic characters in a string to uppercase.
* *
* @param string * NOTE: Mutates the string.
* @return char*
*/
char* string_to_upper(const char* string);
/**
* @brief Replace all the occurrences of search value into replace value in
* the string.
* *
* @param 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, char replace); void string_to_uppercase(char* string);
/**
* @brief Removes all the occurrences of a character in a string.
*
* NOTE: Mutates the string.
*
* @param string
* @param search A character search value.
*/
void string_remove_character(char* string, char search);
/** /**
* @brief Converts a string to its acronym. * @brief Converts a string to its acronym.

View File

@ -1,10 +1,15 @@
#include "character.h" #include "character.h"
#include <stdlib.h> void character_append(char* string, const char character) {
#include <string.h> size_t length = string_get_length(string);
character_append_at(string, character, length);
}
void character_append(char* string, char character) { void character_append_at(char* string, const char character, const size_t index) {
size_t length = strlen(string); size_t length = string_get_length(string);
string[length] = character; for (size_t index_string = length; index_string > index; index_string--) {
string[index_string] = string[index_string - 1];
}
string[index] = character;
string[length + 1] = '\0'; string[length + 1] = '\0';
} }

View File

@ -1,6 +1,11 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
#include "string.h"
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.
@ -10,4 +15,13 @@
*/ */
void character_append(char* string, char character); void character_append(char* string, char character);
/**
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
*
* @param string
* @param character
* @param index
*/
void character_append_at(char* string, const char character, const size_t index);
#endif #endif

View File

@ -1,8 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h" #include "character.h"
char* input() { char* input() {

View File

@ -1,6 +1,9 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -9,8 +9,9 @@ int main() {
char* string = input(); char* string = input();
int shift; int shift;
scanf("%d", &shift); scanf("%d", &shift);
string = string_caesar_cipher(string, shift); char* result = string_caesar_cipher(string, shift);
printf("%s\n", string); printf("%s\n", result);
free(string); free(string);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,24 +1,27 @@
#include "string.h" #include "string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h" #include "character.h"
#define ALPHABET_LENGTH 26 size_t string_get_length(const char* string) {
size_t length = 0;
while (string[length] != '\0') {
length++;
}
return length;
}
char* string_shift_alphabet(int shift) { char* string_shift_alphabet(int shift) {
char* result = malloc(sizeof(char) * (ALPHABET_LENGTH + 1)); char* result = malloc(sizeof(char) * (ALPHABET_LENGTH + 1));
for (char index = 0; index < ALPHABET_LENGTH; index++) { for (size_t index = 0; index < ALPHABET_LENGTH; index++) {
char letter = 'A' + index + shift; char letter = 'A' + index + shift;
if (letter < 'A') { if (letter < 'A') {
letter = 'Z' + shift + index + 1; letter = 'Z' + shift + index + 1;
} else if (letter > 'Z') { } else if (letter > 'Z') {
letter = letter - ALPHABET_LENGTH; letter = letter - ALPHABET_LENGTH;
} }
character_append(result, letter); result[index] = letter;
} }
result[ALPHABET_LENGTH] = '\0';
return result; return result;
} }
@ -28,8 +31,8 @@ char* string_caesar_cipher(char* string, int shift) {
char* shifted_alphabet = string_shift_alphabet(shift); char* shifted_alphabet = string_shift_alphabet(shift);
for (size_t index = 0; index < string_length; index++) { for (size_t index = 0; index < string_length; index++) {
char letter = string[index]; char letter = string[index];
if (letter != ' ') { if (letter != ' ' && (letter >= 'A' && letter <= 'Z')) {
for (int index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) { for (size_t index_alphabet = 0; index_alphabet < ALPHABET_LENGTH; index_alphabet++) {
char current_letter = 'A' + index_alphabet; char current_letter = 'A' + index_alphabet;
if (string[index] == current_letter) { if (string[index] == current_letter) {
letter = shifted_alphabet[index_alphabet]; letter = shifted_alphabet[index_alphabet];
@ -37,7 +40,9 @@ char* string_caesar_cipher(char* string, int shift) {
} }
} }
} }
character_append(result, letter); result[index] = letter;
} }
result[string_length] = '\0';
free(shifted_alphabet);
return result; return result;
} }

View File

@ -1,8 +1,20 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define ALPHABET_LENGTH 26
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
/**
* @brief Return the length of a string (excluding '\0').
*
* @param string
*/
size_t string_get_length(const char* string);
/** /**
* @brief Shift the alphabet by a given amount. * @brief Shift the alphabet by a given amount.
* *

View File

@ -1,20 +1,22 @@
#include "character.h" #include "character.h"
#include <stdlib.h> void character_append(char* string, const char character) {
#include <string.h> size_t length = string_get_length(string);
character_append_at(string, character, length);
}
void character_append(char* string, char character) { void character_append_at(char* string, const char character, const size_t index) {
size_t length = strlen(string); size_t length = string_get_length(string);
string[length] = character; for (size_t index_string = length; index_string > index; index_string--) {
string[index_string] = string[index_string - 1];
}
string[index] = character;
string[length + 1] = '\0'; string[length + 1] = '\0';
} }
char character_to_upper(char character) { char character_to_upper(const char character) {
char ascii_a = 'a'; if (character >= 'a' && character <= 'z') {
char ascii_A = 'A'; return character + ('A' - 'a');
char ascii_z = 'z';
if (character >= ascii_a && character <= ascii_z) {
return character + (ascii_A - ascii_a);
} }
return character; return character;
} }

View File

@ -1,6 +1,12 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include "string.h"
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.
@ -10,12 +16,20 @@
*/ */
void character_append(char* string, char character); void character_append(char* string, char character);
/**
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
*
* @param string
* @param character
* @param index
*/
void character_append_at(char* string, const char character, const size_t index);
/** /**
* @brief Converts the character to uppercase. * @brief Converts the character to uppercase.
* *
* @param character * @param character
* @return char
*/ */
char character_to_upper(char character); char character_to_upper(const char character);
#endif #endif

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -7,9 +7,10 @@
int main() { int main() {
char* string = input(); char* string = input();
string = string_trim(string); string_trim(string, ' ');
string = string_camelCase(string); char* result = string_camelCase(string);
printf("%s\n", string); printf("%s\n", result);
free(string); free(string);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,66 +1,73 @@
#include "string.h" #include "string.h"
#include <stdbool.h> size_t string_get_length(const char* string) {
#include <stdlib.h> size_t length = 0;
#include <string.h> while (string[length] != '\0') {
length++;
}
return length;
}
#include "character.h" void string_trim_start(char* string, char character) {
size_t string_length = string_get_length(string);
char* string_trim_start(char* string) {
size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1));
size_t index_space = 0; size_t index_space = 0;
while (string[index_space] == ' ') { while (string[index_space] == character) {
index_space++; index_space++;
} }
for (size_t index = index_space; index < string_length; index++) { for (size_t index = 0; index < string_length - index_space; index++) {
character_append(result, string[index]); string[index] = string[index + index_space];
} }
return result; string[string_length - index_space] = '\0';
} }
char* string_trim_end(char* string) { void string_trim_end(char* string, char character) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1));
size_t index_space = string_length - 1; size_t index_space = string_length - 1;
while (string[index_space] == ' ') { while (string[index_space] == character) {
index_space--; index_space--;
} }
for (size_t index = 0; index < index_space + 1; index++) { string[index_space + 1] = '\0';
character_append(result, string[index]);
}
return result;
} }
char* string_trim(char* string) { void string_trim(char* string, char character) {
char* result = string_trim_start(string); string_trim_start(string, character);
result = string_trim_end(result); string_trim_end(string, character);
return result;
} }
char* string_capitalize(char* string) { void string_capitalize(char* string) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
if (string_length > 0) { if (string_length == 0) {
return;
}
string[0] = character_to_upper(string[0]); string[0] = character_to_upper(string[0]);
} }
return string;
}
char* string_camelCase(char* string) { char* string_camelCase(char* string) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1)); char* result = malloc(sizeof(char) * (string_length + 1));
int words = 0; result[0] = '\0';
size_t words = 0;
char* current = malloc(sizeof(char) * (string_length + 1)); char* current = malloc(sizeof(char) * (string_length + 1));
size_t current_index = 0;
for (size_t index = 0; index < string_length; index++) { for (size_t index = 0; index < string_length; index++) {
if (string[index] == ' ') { if (string[index] == ' ') {
strcat(result, words == 0 ? current : string_capitalize(current)); current[current_index] = '\0';
if (words > 0) {
string_capitalize(current);
}
strcat(result, current);
memset(current, 0, sizeof(char) * (string_length + 1)); memset(current, 0, sizeof(char) * (string_length + 1));
words++; current_index = 0;
words += 1;
} else { } else {
character_append(current, string[index]); current[current_index] = string[index];
current_index += 1;
} }
} }
strcat(result, words == 0 ? current : string_capitalize(current)); if (words > 0) {
string_capitalize(current);
strcat(result, current);
}
free(current); free(current);
return result; return result;
} }

View File

@ -1,29 +1,46 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
/** #include <stdbool.h>
* @brief Removes all whitespace from the start of a string. #include <stdlib.h>
* #include <string.h>
* @param string
* @return char* #include "character.h"
*/
char* string_trim_start(char* string);
/** /**
* @brief Removes all whitespace from the end of a string. * @brief Return the length of a string (excluding '\0').
* *
* @param string * @param string
* @return char* * @return size_t
*/ */
char* string_trim_end(char* string); size_t string_get_length(const char* string);
/** /**
* @brief Removes all whitespace from the start and end of a string. * @brief Removes all `character` from the start of a string.
*
* NOTE: Mutates the string.
* *
* @param string * @param string
* @return char*
*/ */
char* string_trim(char* string); void string_trim_start(char* string, char character);
/**
* @brief Removes all `character` from the end of a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_trim_end(char* string, char character);
/**
* @brief Removes all `character` from the start and end of a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_trim(char* string, char character);
/** /**
* @brief Converts a string to camel case. * @brief Converts a string to camel case.
@ -36,9 +53,10 @@ char* string_camelCase(char* string);
/** /**
* @brief Capitalizes the string. * @brief Capitalizes the string.
* *
* NOTE: Mutates the string.
*
* @param string * @param string
* @return char*
*/ */
char* string_capitalize(char* string); void string_capitalize(char* string);
#endif #endif

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -4,15 +4,11 @@
#include "input.h" #include "input.h"
int math_power(int base, int power) { unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent) {
int result = 1; return exponent == 0 ? 1 : base * mathematics_pow(base, exponent - 1);
for (int iteration = 0; iteration < power; iteration++) {
result = result * base;
}
return result;
} }
char* convert_from_base_10_to_base(unsigned long number, unsigned int base) { char* convert_number_from_base_10_to_base(unsigned long long number, unsigned long base) {
if (number == 0) { if (number == 0) {
return "0"; return "0";
} }
@ -34,13 +30,14 @@ char* convert_from_base_10_to_base(unsigned long number, unsigned int base) {
} }
index_result++; index_result++;
} }
result[index_result] = '\0';
return result; return result;
} }
int convert_from_base_to_base_10(char* number, unsigned int base) { unsigned long convert_number_from_base_to_base_10(char* number, unsigned long base) {
int length = strlen(number); size_t length = strlen(number);
int exponent = length - 1; int exponent = length - 1;
int result = 0; unsigned long result = 0;
int index = 0; int index = 0;
while (exponent >= 0) { while (exponent >= 0) {
int current_number = (int)(number[index] - '0'); int current_number = (int)(number[index] - '0');
@ -49,19 +46,26 @@ int convert_from_base_to_base_10(char* number, unsigned int base) {
} else { } else {
current_number = (int)(number[index] - '0'); current_number = (int)(number[index] - '0');
} }
result = result + current_number * math_power(base, exponent); result = result + current_number * mathematics_pow(base, exponent);
exponent--; exponent--;
index++; index++;
} }
return result; return result;
} }
char* convert_number_from_base_to_another(char* number, unsigned long base_from, unsigned long base_target) {
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
}
int main() { int main() {
char* number = input(); char* number = input();
int base_from; unsigned long base_from;
int base_target; unsigned long base_target;
scanf("%d", &base_from); scanf("%lu", &base_from);
scanf("%d", &base_target); scanf("%lu", &base_target);
printf("%s\n", convert_from_base_10_to_base(convert_from_base_to_base_10(number, base_from), base_target)); char* result = convert_number_from_base_to_another(number, base_from, base_target);
printf("%s\n", result);
free(number);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,13 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int fibonacci(int number) { unsigned long fibonacci(unsigned long number) {
return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2); return number < 2 ? number : fibonacci(number - 1) + fibonacci(number - 2);
} }
int main() { int main() {
int number; unsigned long number;
scanf("%d", &number); scanf("%lu", &number);
printf("%d\n", fibonacci(number)); printf("%lu\n", fibonacci(number));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -1,11 +1,5 @@
#include "string.h" #include "string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
size_t string_total_occurrences_of_character(char* string, char character) { size_t string_total_occurrences_of_character(char* string, char character) {
size_t result = 0; size_t result = 0;
size_t string_length = strlen(string); size_t string_length = strlen(string);

View File

@ -1,7 +1,11 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "character.h"
/** /**
* @brief Returns the total number of occurrences of the given character in the string. * @brief Returns the total number of occurrences of the given character in the string.

View File

@ -3,9 +3,9 @@
#include <stdlib.h> #include <stdlib.h>
int main() { int main() {
int length; unsigned long length;
scanf("%d", &length); scanf("%lu", &length);
for (int number = 1; number <= length; number++) { for (unsigned long number = 1; number <= length; number++) {
bool is_divisible_by_3 = number % 3 == 0; bool is_divisible_by_3 = number % 3 == 0;
bool is_divisible_by_5 = number % 5 == 0; bool is_divisible_by_5 = number % 5 == 0;
if (is_divisible_by_3 && is_divisible_by_5) { if (is_divisible_by_3 && is_divisible_by_5) {
@ -15,7 +15,7 @@ int main() {
} else if (is_divisible_by_5) { } else if (is_divisible_by_5) {
printf("Buzz\n"); printf("Buzz\n");
} else { } else {
printf("%d\n", number); printf("%lu\n", number);
} }
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -6,5 +6,6 @@
int main() { int main() {
char *string = input(); char *string = input();
printf("Hello, %s!\n", string); printf("Hello, %s!\n", string);
free(string);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -9,8 +9,8 @@
int main() { int main() {
char* string = input(); char* string = input();
string = string_to_upper(string); string_to_uppercase(string);
string = string_replace(string, ' ', '\0'); string_remove_character(string, ' ');
bool is_palindrome = string_is_palindrome(string); bool is_palindrome = string_is_palindrome(string);
free(string); free(string);
printf("%s\n", is_palindrome ? "true" : "false"); printf("%s\n", is_palindrome ? "true" : "false");

View File

@ -1,46 +1,67 @@
#include "string.h" #include "string.h"
#include <stdbool.h> size_t string_get_length(const char* string) {
#include <stdlib.h> size_t length = 0;
#include <string.h> while (string[length] != '\0') {
length++;
}
return length;
}
#include "character.h" void string_to_uppercase(char* string) {
size_t string_length = string_get_length(string);
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++) { for (size_t index = 0; index < string_length; index++) {
character_append(result, character_to_upper(string[index])); string[index] = character_to_upper(string[index]);
} }
return result; string[string_length] = '\0';
} }
char* string_reverse(const char* string) { void string_remove_character(char* string, char search) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1)); for (size_t index = 0; index < string_length; index++) {
for (int index = string_length - 1; index != -1; index--) { if (string[index] == search) {
character_append(result, string[index]); for (size_t index_string = index; index_string < string_length; index_string++) {
string[index_string] = string[index_string + 1];
} }
return result; string_length--;
index--;
}
}
string[string_length] = '\0';
} }
bool string_is_palindrome(const char* string) { char* string_copy(const char* string) {
char* string_reversed = string_reverse(string); size_t source_length = string_get_length(string);
bool is_palindrome = strcmp(string_reversed, string) == 0; char* copy = malloc(sizeof(char) * (source_length + 1));
if (copy == NULL) {
perror("Error (string_copy)");
exit(EXIT_FAILURE);
}
size_t index;
for (index = 0; index < source_length; index++) {
copy[index] = string[index];
}
copy[index] = '\0';
return copy;
}
void string_reverse(char* string) {
size_t string_length = string_get_length(string);
size_t index_start = 0;
size_t index_end = string_length - 1;
while (index_start < index_end) {
char temporary = string[index_start];
string[index_start] = string[index_end];
string[index_end] = temporary;
index_start++;
index_end--;
}
}
bool string_is_palindrome(char* string) {
char* string_reversed = string_copy(string);
string_reverse(string_reversed);
bool is_palindrome = strcmp(string, string_reversed) == 0;
free(string_reversed); free(string_reversed);
return is_palindrome; return is_palindrome;
} }
char* string_replace(const char* string, char search, char replace) {
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 == string[index];
if (is_search_value) {
character_append(result, replace);
} else {
character_append(result, string[index]);
}
}
return result;
}

View File

@ -1,23 +1,57 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
#include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
/**
* @brief Return the length of a string (excluding '\0').
*
* @param string
* @return size_t
*/
size_t string_get_length(const char* string);
/** /**
* @brief Converts all the alphabetic characters in a string to uppercase. * @brief Converts all the alphabetic characters in a string to uppercase.
* *
* NOTE: Mutates the string.
*
* @param string * @param string
* @return char*
*/ */
char* string_to_upper(const char* string); void string_to_uppercase(char* string);
/** /**
* @brief Reverse the characters in an array. * @brief Removes all the occurrences of a character in a string.
*
* NOTE: Mutates the string.
*
* @param string
* @param search A character search value.
*/
void string_remove_character(char* string, char search);
/**
* @brief Reverse the characters in a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_reverse(char* string);
/**
* @brief Return the copy of a string.
* *
* @param string * @param string
* @return char* * @return char*
*/ */
char* string_reverse(const char* string); char* string_copy(const char* string);
/** /**
* @brief Returns true if the string is a palindrome (a palindrome is a word, * @brief Returns true if the string is a palindrome (a palindrome is a word,
@ -27,17 +61,6 @@ char* string_reverse(const char* string);
* @param string The string to check. * @param string The string to check.
* @return true if the string is a palindrome, false otherwise. * @return true if the string is a palindrome, false otherwise.
*/ */
bool string_is_palindrome(const char* string); bool string_is_palindrome(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, char replace);
#endif #endif

View File

@ -2,8 +2,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
bool is_prime_number(int number) { bool is_prime_number(unsigned long number) {
for (int iteration = 2; iteration < number; iteration++) { for (unsigned long iteration = 2; iteration < number; iteration++) {
if (number % iteration == 0) { if (number % iteration == 0) {
return false; return false;
} }
@ -12,8 +12,8 @@ bool is_prime_number(int number) {
} }
int main() { int main() {
int number; unsigned long number;
scanf("%d", &number); scanf("%lu", &number);
bool is_prime = is_prime_number(number); bool is_prime = is_prime_number(number);
printf("%s\n", is_prime ? "true" : "false"); printf("%s\n", is_prime ? "true" : "false");
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -24,5 +24,7 @@ int main() {
index += number_of_appearances - 1; index += number_of_appearances - 1;
} }
printf("%s\n", result); printf("%s\n", result);
free(string);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -1,11 +1,5 @@
#include "string.h" #include "string.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
bool string_starts_with(char* string, char* prefix) { bool string_starts_with(char* string, char* prefix) {
size_t string_length = strlen(string); size_t string_length = strlen(string);
size_t prefix_length = strlen(prefix); size_t prefix_length = strlen(prefix);

View File

@ -2,6 +2,10 @@
#define __STRING__ #define __STRING__
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
/** /**
* @brief Returns true if the string is a prefix of another string. * @brief Returns true if the string is a prefix of another string.

View File

@ -28,8 +28,10 @@ int* get_dividers_list(int number, int* dividers_length) {
bool is_prime_number(int number) { bool is_prime_number(int number) {
int dividers_length; int dividers_length;
get_dividers_list(number, &dividers_length); int* dividers_list = get_dividers_list(number, &dividers_length);
return dividers_length == 2; bool result = dividers_length == 2;
free(dividers_list);
return result;
} }
int sum_multiply_numbers(int* numbers, int numbers_length) { int sum_multiply_numbers(int* numbers, int numbers_length) {

View File

@ -1,9 +1,5 @@
#include "character.h" #include "character.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,7 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -7,10 +7,10 @@
int main() { int main() {
char* type = input(); char* type = input();
int height; unsigned long height;
scanf("%d", &height); scanf("%lu", &height);
int step = strcmp(type, "normal") == 0 ? 1 : height; unsigned long step = strcmp(type, "normal") == 0 ? 1 : height;
while ((strcmp(type, "normal") == 0 && step <= height) || (strcmp(type, "reverse") == 0 && step != 0)) { while ((strcmp(type, "normal") == 0 && step <= height) || (strcmp(type, "reverse") == 0 && step != 0)) {
size_t numberOfStars = (step * 2) - 1; size_t numberOfStars = (step * 2) - 1;
size_t totalNumberOfLocations = (height * 2) - 1; size_t totalNumberOfLocations = (height * 2) - 1;
@ -23,5 +23,6 @@ int main() {
step = strcmp(type, "normal") == 0 ? step + 1 : step - 1; step = strcmp(type, "normal") == 0 ? step + 1 : step - 1;
} }
free(type);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -20,7 +20,7 @@ bool is_integer(char* string) {
int main() { int main() {
char* string = input(); char* string = input();
struct Stack* stack = stack_initialization(); struct stack* stack = stack_initialization();
char* token = strtok(string, " "); char* token = strtok(string, " ");
while (token != NULL) { while (token != NULL) {
if (is_integer(token)) { if (is_integer(token)) {
@ -45,5 +45,7 @@ int main() {
token = strtok(NULL, " "); token = strtok(NULL, " ");
} }
printf("%ld\n", (intptr_t)stack_pop(stack)); printf("%ld\n", (intptr_t)stack_pop(stack));
free(string);
stack_free(stack);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,11 +1,9 @@
#include "stack.h" #include "stack.h"
#include <stdio.h> struct stack *stack_initialization() {
#include <stdlib.h> struct stack *stack = malloc(sizeof(struct stack));
struct Stack *stack_initialization() {
struct Stack *stack = malloc(sizeof(*stack));
if (stack == NULL) { if (stack == NULL) {
perror("Error (stack_initialization)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
stack->first = NULL; stack->first = NULL;
@ -13,9 +11,15 @@ struct Stack *stack_initialization() {
return stack; return stack;
} }
void stack_push(struct Stack *stack, void *data) { void stack_push(struct stack *stack, void *data) {
struct Node *node_new = malloc(sizeof(*node_new)); if (stack == NULL) {
if (stack == NULL || data == NULL) { errno = EINVAL;
perror("Error (stack_push)");
exit(EXIT_FAILURE);
}
struct stack_node *node_new = malloc(sizeof(struct stack_node));
if (data == NULL) {
perror("Error (stack_push)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
node_new->data = data; node_new->data = data;
@ -24,11 +28,13 @@ void stack_push(struct Stack *stack, void *data) {
stack->length = stack->length + 1; stack->length = stack->length + 1;
} }
void *stack_pop(struct Stack *stack) { void *stack_pop(struct stack *stack) {
if (stack == NULL) { if (stack == NULL) {
errno = EINVAL;
perror("Error (stack_pop)");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct Node *node = stack->first; struct stack_node *node = stack->first;
void *data = NULL; void *data = NULL;
if (node != NULL) { if (node != NULL) {
stack->first = node->next; stack->first = node->next;
@ -38,3 +44,18 @@ void *stack_pop(struct Stack *stack) {
stack->length = stack->length - 1; stack->length = stack->length - 1;
return data; return data;
} }
void stack_free(struct stack *stack) {
if (stack == NULL) {
errno = EINVAL;
perror("Error (stack_free)");
exit(EXIT_FAILURE);
}
struct stack_node *node = stack->first;
while (node != NULL) {
struct stack_node *node_next = node->next;
free(node);
node = node_next;
}
free(stack);
}

View File

@ -1,23 +1,54 @@
#ifndef __STACK__ #ifndef __STACK__
#define __STACK__ #define __STACK__
#include <errno.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// LIFO = Last In First Out /**
struct Stack { * @brief Stack structure => LIFO (Last In First Out).
struct Node *first; */
struct stack {
struct stack_node *first;
size_t length; size_t length;
}; };
struct Node { /**
* @brief Stack node structure.
*/
struct stack_node {
void *data; void *data;
struct Node *next; struct stack_node *next;
}; };
struct Stack *stack_initialization(); /**
* @brief Stack initialization.
*
* @return struct stack*
*/
struct stack *stack_initialization();
void stack_push(struct Stack *stack, void *data); /**
* @brief Push data to stack.
*
* @param stack
* @param data
*/
void stack_push(struct stack *stack, void *data);
void *stack_pop(struct Stack *stack); /**
* @brief Pop data from stack.
*
* @param stack
* @return void*
*/
void *stack_pop(struct stack *stack);
/**
* @brief Frees the stack.
*
* @param stack
*/
void stack_free(struct stack *stack);
#endif #endif

View File

@ -1,16 +1,10 @@
#include "array_2D_int.h" #include "array_2D_int.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns) { void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_columns) {
for (size_t i = 0; i < number_of_rows; i++) { for (size_t row = 0; row < number_of_rows; row++) {
for (size_t j = 0; j < number_of_columns; j++) { for (size_t column = 0; column < number_of_columns; column++) {
printf("%d", array[i][j]); printf("%d", array[row][column]);
if (j != number_of_columns - 1) { if (column != number_of_columns - 1) {
printf(" "); printf(" ");
} }
} }
@ -19,7 +13,7 @@ void array_2D_int_print(int **array, size_t number_of_rows, size_t number_of_col
} }
int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) { int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) {
int **array = malloc(sizeof(int *)); int **array = malloc(sizeof(int));
*number_of_rows = 1; *number_of_rows = 1;
*number_of_columns = 1; *number_of_columns = 1;
array[0] = malloc(*number_of_columns * sizeof(int)); array[0] = malloc(*number_of_columns * sizeof(int));
@ -60,13 +54,13 @@ int **array_2D_int_input(size_t *number_of_rows, size_t *number_of_columns) {
} }
int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) { int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *number_of_columns) {
int **rotated_array = malloc(*number_of_columns * sizeof(int *)); int **rotated_array = malloc(*number_of_rows * sizeof(int));
for (size_t i = 0; i < *number_of_columns; i++) { for (size_t row = 0; row < *number_of_rows; row++) {
rotated_array[i] = malloc(*number_of_rows * sizeof(int)); rotated_array[row] = malloc(*number_of_columns * sizeof(int));
} }
for (size_t i = 0; i < *number_of_columns; i++) { for (size_t row = 0; row < *number_of_columns; row++) {
for (size_t j = 0; j < *number_of_rows; j++) { for (size_t column = 0; column < *number_of_rows; column++) {
rotated_array[i][j] = array[*number_of_rows - i - 1][j]; rotated_array[row][column] = array[*number_of_rows - row - 1][column];
} }
} }
return rotated_array; return rotated_array;
@ -74,12 +68,12 @@ int **array_2D_int_reverse_rows(int **array, size_t *number_of_rows, size_t *num
int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) {
int **rotated_array = malloc(*number_of_columns * sizeof(int *)); int **rotated_array = malloc(*number_of_columns * sizeof(int *));
for (size_t i = 0; i < *number_of_columns; i++) { for (size_t row = 0; row < *number_of_columns; row++) {
rotated_array[i] = malloc(*number_of_rows * sizeof(int)); rotated_array[row] = malloc(*number_of_rows * sizeof(int));
} }
for (size_t i = 0; i < *number_of_columns; i++) { for (size_t row = 0; row < *number_of_columns; row++) {
for (size_t j = 0; j < *number_of_rows; j++) { for (size_t column = 0; column < *number_of_rows; column++) {
rotated_array[i][j] = array[*number_of_rows - j - 1][i]; rotated_array[row][column] = array[*number_of_rows - column - 1][row];
} }
} }
size_t number_of_rows_temp = *number_of_rows; size_t number_of_rows_temp = *number_of_rows;
@ -89,9 +83,28 @@ int **array_2D_int_rotate_90_degrees_clockwise(int **array, size_t *number_of_ro
} }
int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) { int **array_2D_int_rotate_90_degrees_anticlockwise(int **array, size_t *number_of_rows, size_t *number_of_columns) {
int **result = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns); int **result_1 = array_2D_int_rotate_90_degrees_clockwise(array, number_of_rows, number_of_columns);
result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns); size_t number_of_rows_temp = *number_of_rows;
result = array_2D_int_rotate_90_degrees_clockwise(result, number_of_rows, number_of_columns);
result = array_2D_int_reverse_rows(result, number_of_rows, number_of_columns); int **result_2 = array_2D_int_rotate_90_degrees_clockwise(result_1, number_of_rows, number_of_columns);
return result; for (size_t row = 0; row < number_of_rows_temp; row++) {
free(result_1[row]);
}
free(result_1);
number_of_rows_temp = *number_of_rows;
int **result_3 = array_2D_int_rotate_90_degrees_clockwise(result_2, number_of_rows, number_of_columns);
for (size_t row = 0; row < number_of_rows_temp; row++) {
free(result_2[row]);
}
free(result_2);
number_of_rows_temp = *number_of_rows;
int **result_4 = array_2D_int_reverse_rows(result_3, number_of_rows, number_of_columns);
for (size_t row = 0; row < number_of_rows_temp; row++) {
free(result_3[row]);
}
free(result_3);
return result_4;
} }

View File

@ -1,7 +1,11 @@
#ifndef __ARRAY_2D_INT__ #ifndef __ARRAY_2D_INT__
#define __ARRAY_2D_INT__ #define __ARRAY_2D_INT__
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "character.h"
/** /**
* @brief Prints a 2D array of integers. * @brief Prints a 2D array of integers.

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -8,19 +8,20 @@ int main() {
size_t number_of_rows = 0; size_t number_of_rows = 0;
size_t number_of_columns = 0; size_t number_of_columns = 0;
char *direction = input(); char *direction = input();
int **array = array_2D_int_input(&number_of_rows, &number_of_columns); int **array_input = array_2D_int_input(&number_of_rows, &number_of_columns);
int **array;
if (strcmp(direction, "clockwise") == 0) { if (strcmp(direction, "clockwise") == 0) {
array = array_2D_int_rotate_90_degrees_clockwise(array, &number_of_rows, &number_of_columns); array = array_2D_int_rotate_90_degrees_clockwise(array_input, &number_of_rows, &number_of_columns);
} else { } else {
array = array_2D_int_rotate_90_degrees_anticlockwise(array, &number_of_rows, &number_of_columns); array = array_2D_int_rotate_90_degrees_anticlockwise(array_input, &number_of_rows, &number_of_columns);
} }
array_2D_int_print(array, number_of_rows, number_of_columns); array_2D_int_print(array, number_of_rows, number_of_columns);
for (size_t i = 0; i < number_of_rows; i++) { free(direction);
free(array[i]); for (size_t row = 0; row < number_of_rows; row++) {
free(array[row]);
} }
free(array); free(array);
free(direction);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,9 +1,5 @@
#include "character.h" #include "character.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -2,6 +2,8 @@
#define __CHARACTER__ #define __CHARACTER__
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -7,11 +7,12 @@
int main() { int main() {
char* string = input(); char* string = input();
string = string_trim(string, ' '); string_trim(string, ' ');
string = string_trim(string, '-'); string_trim(string, '-');
string = string_to_lowercase(string); string_to_lowercase(string);
string = string_slugify(string); char* result = string_slugify(string);
printf("%s\n", string); printf("%s\n", result);
free(string); free(string);
free(result);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -1,66 +1,67 @@
#include "string.h" #include "string.h"
#include <stdbool.h> size_t string_get_length(const char* string) {
#include <stdlib.h> size_t length = 0;
#include <string.h> while (string[length] != '\0') {
length++;
}
return length;
}
#include "character.h" void string_to_lowercase(char* string) {
size_t string_length = string_get_length(string);
for (size_t index = 0; index < string_length; index++) {
string[index] = character_to_lower(string[index]);
}
string[string_length] = '\0';
}
char* string_trim_start(char* string, char character) { void string_trim_start(char* string, char character) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1));
size_t index_space = 0; size_t index_space = 0;
while (string[index_space] == character) { while (string[index_space] == character) {
index_space++; index_space++;
} }
for (size_t index = index_space; index < string_length; index++) { for (size_t index = 0; index < string_length - index_space; index++) {
character_append(result, string[index]); string[index] = string[index + index_space];
} }
return result; string[string_length - index_space] = '\0';
} }
char* string_trim_end(char* string, char character) { void string_trim_end(char* string, char character) {
size_t string_length = strlen(string); size_t string_length = string_get_length(string);
char* result = malloc(sizeof(char) * (string_length + 1));
size_t index_space = string_length - 1; size_t index_space = string_length - 1;
while (string[index_space] == character) { while (string[index_space] == character) {
index_space--; index_space--;
} }
for (size_t index = 0; index < index_space + 1; index++) { string[index_space + 1] = '\0';
character_append(result, string[index]);
}
return result;
} }
char* string_trim(char* string, char character) { void string_trim(char* string, char character) {
char* result = string_trim_start(string, character); string_trim_start(string, character);
result = string_trim_end(result, character); string_trim_end(string, character);
return result;
}
char* string_to_lowercase(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_lower(string[index]));
}
return result;
} }
char* string_slugify(char* string) { char* string_slugify(char* string) {
size_t string_length = strlen(string); size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1)); char* result = malloc(sizeof(char) * (string_length + 1));
int words = 0; result[0] = '\0';
size_t words = 0;
char* current = malloc(sizeof(char) * (string_length + 1)); char* current = malloc(sizeof(char) * (string_length + 1));
current[0] = '\0';
size_t current_index = 0;
for (size_t index = 0; index < string_length; index++) { for (size_t index = 0; index < string_length; index++) {
if (string[index] == ' ' || (string[index] == '-' && strlen(current) > 0)) { if (string[index] == ' ' || (string[index] == '-' && strlen(current) > 0)) {
strcat(result, current); strcat(result, current);
character_append(result, '-'); character_append(result, '-');
memset(current, 0, sizeof(char) * (string_length + 1)); memset(current, 0, sizeof(char) * (string_length + 1));
words++; current_index = 0;
words += 1;
} else { } else {
if (character_is_alphanumeric(string[index])) { if (character_is_alphanumeric(string[index])) {
character_append(current, string[index]); current[current_index] = string[index];
current_index += 1;
current[current_index] = '\0';
} }
} }
} }

View File

@ -1,37 +1,55 @@
#ifndef __STRING__ #ifndef __STRING__
#define __STRING__ #define __STRING__
/** #include <stdbool.h>
* @brief Removes all `character` from the start of a string. #include <stdlib.h>
* #include <string.h>
* @param string
* @return char* #include "character.h"
*/
char* string_trim_start(char* string, char character);
/** /**
* @brief Removes all `character` from the end of a string. * @brief Return the length of a string (excluding '\0').
* *
* @param string * @param string
* @return char* * @return size_t
*/ */
char* string_trim_end(char* string, char character); size_t string_get_length(const char* string);
/**
* @brief Removes all `character` from the start and end of a string.
*
* @param string
* @return char*
*/
char* string_trim(char* string, char character);
/** /**
* @brief Converts all the alphabetic characters in a string to lowercase. * @brief Converts all the alphabetic characters in a string to lowercase.
* *
* NOTE: Mutates the string.
*
* @param string * @param string
* @return char*
*/ */
char* string_to_lowercase(char* string); void string_to_lowercase(char* string);
/**
* @brief Removes all `character` from the start of a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_trim_start(char* string, char character);
/**
* @brief Removes all `character` from the end of a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_trim_end(char* string, char character);
/**
* @brief Removes all `character` from the start and end of a string.
*
* NOTE: Mutates the string.
*
* @param string
*/
void string_trim(char* string, char character);
/** /**
* @brief Generate a slug from a string. * @brief Generate a slug from a string.

View File

@ -1,8 +1,5 @@
#include "character.h" #include "character.h"
#include <stdlib.h>
#include <string.h>
void character_append(char* string, char character) { void character_append(char* string, char character) {
size_t length = strlen(string); size_t length = strlen(string);
string[length] = character; string[length] = character;

View File

@ -1,6 +1,9 @@
#ifndef __CHARACTER__ #ifndef __CHARACTER__
#define __CHARACTER__ #define __CHARACTER__
#include <stdlib.h>
#include <string.h>
/** /**
* @brief Append a character to a string, assuming string points to an array * @brief Append a character to a string, assuming string points to an array
* with enough space. * with enough space.

View File

@ -1,10 +1,5 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
char* input() { char* input() {
char character; char character;
size_t length = 1; size_t length = 1;

View File

@ -1,6 +1,11 @@
#ifndef __INPUT__ #ifndef __INPUT__
#define __INPUT__ #define __INPUT__
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
/** /**
* @brief Read a line from stdin. * @brief Read a line from stdin.
* *

View File

@ -6,5 +6,6 @@
int main() { int main() {
char *string = input(); char *string = input();
printf("Hello, %s!\n", string); printf("Hello, %s!\n", string);
free(string);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }