mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add camel-case/c/function
This commit is contained in:
parent
a1a0c65c16
commit
0c078abb89
3
challenges/camel-case/solutions/c/function/README.md
Normal file
3
challenges/camel-case/solutions/c/function/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# camel-case/c/function
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 29 September 2021.
|
20
challenges/camel-case/solutions/c/function/character.c
Normal file
20
challenges/camel-case/solutions/c/function/character.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#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) {
|
||||||
|
char ascii_a = 'a';
|
||||||
|
char ascii_A = 'A';
|
||||||
|
char ascii_z = 'z';
|
||||||
|
if (character >= ascii_a && character <= ascii_z) {
|
||||||
|
return character + (ascii_A - ascii_a);
|
||||||
|
}
|
||||||
|
return character;
|
||||||
|
}
|
21
challenges/camel-case/solutions/c/function/character.h
Normal file
21
challenges/camel-case/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
|
19
challenges/camel-case/solutions/c/function/input.c
Normal file
19
challenges/camel-case/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/camel-case/solutions/c/function/input.h
Normal file
11
challenges/camel-case/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
|
15
challenges/camel-case/solutions/c/function/solution.c
Normal file
15
challenges/camel-case/solutions/c/function/solution.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char* string = input();
|
||||||
|
string = string_trim(string);
|
||||||
|
string = string_camelCase(string);
|
||||||
|
printf("%s\n", string);
|
||||||
|
free(string);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
66
challenges/camel-case/solutions/c/function/string.c
Normal file
66
challenges/camel-case/solutions/c/function/string.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
|
||||||
|
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;
|
||||||
|
while (string[index_space] == ' ') {
|
||||||
|
index_space++;
|
||||||
|
}
|
||||||
|
for (size_t index = index_space; index < string_length; index++) {
|
||||||
|
character_append(result, string[index]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* string_trim_end(char* string) {
|
||||||
|
size_t string_length = strlen(string);
|
||||||
|
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||||
|
size_t index_space = string_length - 1;
|
||||||
|
while (string[index_space] == ' ') {
|
||||||
|
index_space--;
|
||||||
|
}
|
||||||
|
for (size_t index = 0; index < index_space + 1; index++) {
|
||||||
|
character_append(result, string[index]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* string_trim(char* string) {
|
||||||
|
char* result = string_trim_start(string);
|
||||||
|
result = string_trim_end(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* string_capitalize(char* string) {
|
||||||
|
size_t string_length = strlen(string);
|
||||||
|
if (string_length > 0) {
|
||||||
|
string[0] = character_to_upper(string[0]);
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* string_camelCase(char* string) {
|
||||||
|
size_t string_length = strlen(string);
|
||||||
|
char* result = malloc(sizeof(char) * (string_length + 1));
|
||||||
|
int words = 0;
|
||||||
|
char* current = malloc(sizeof(char) * (string_length + 1));
|
||||||
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
|
if (string[index] == ' ') {
|
||||||
|
strcat(result, words == 0 ? current : string_capitalize(current));
|
||||||
|
memset(current, 0, sizeof(char) * (string_length + 1));
|
||||||
|
words++;
|
||||||
|
} else {
|
||||||
|
character_append(current, string[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strcat(result, words == 0 ? current : string_capitalize(current));
|
||||||
|
free(current);
|
||||||
|
return result;
|
||||||
|
}
|
44
challenges/camel-case/solutions/c/function/string.h
Normal file
44
challenges/camel-case/solutions/c/function/string.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#ifndef STRING_H
|
||||||
|
#define STRING_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Removes all whitespace from the start of a string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* string_trim_start(char* string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Removes all whitespace from the end of a string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* string_trim_end(char* string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Removes all whitespace from the start and end of a string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* string_trim(char* string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Converts a string to camel case.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* string_camelCase(char* string);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Capitalizes the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* string_capitalize(char* string);
|
||||||
|
|
||||||
|
#endif
|
@ -10,10 +10,11 @@ void character_append(char* string, char character) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char character_to_upper(const char character) {
|
const char character_to_upper(const char character) {
|
||||||
int a_ascii_code = (int)'a';
|
char ascii_a = 'a';
|
||||||
int z_ascii_code = (int)'z';
|
char ascii_A = 'A';
|
||||||
if (character >= a_ascii_code && character <= z_ascii_code) {
|
char ascii_z = 'z';
|
||||||
return character - 32;
|
if (character >= ascii_a && character <= ascii_z) {
|
||||||
|
return character + (ascii_A - ascii_a);
|
||||||
}
|
}
|
||||||
return character;
|
return character;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
|
||||||
char* input() {
|
char* input() {
|
||||||
char character;
|
char character;
|
||||||
size_t length = 1;
|
size_t length = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user