1
1
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:
Divlo 2021-09-29 22:20:41 +02:00
parent a1a0c65c16
commit 0c078abb89
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
10 changed files with 206 additions and 4 deletions

View File

@ -0,0 +1,3 @@
# camel-case/c/function
Created by [@Divlo](https://github.com/Divlo) on 29 September 2021.

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

View 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

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_H
#define INPUT_H
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

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

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

View 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

View File

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

View File

@ -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;