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

feat(solutions): add acronyms/c/function

This commit is contained in:
Divlo 2021-09-29 22:34:22 +02:00
parent 0c078abb89
commit 7d140cb8a9
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
8 changed files with 168 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# acronyms/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,16 @@
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
#include "input.h"
#include "string.h"
int main() {
char* string = input();
string = string_replace(string, '"', '\0');
string = string_to_upper(string);
string = string_acronym(string);
printf("%s\n", string);
free(string);
return EXIT_SUCCESS;
}

View 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_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;
}
char* string_acronym(char* string) {
size_t string_length = strlen(string);
char* result = malloc(sizeof(char) * (string_length + 1));
char* current = malloc(sizeof(char) * (string_length + 1));
for (size_t index = 0; index < string_length; index++) {
if (string[index] == ' ') {
character_append(result, current[0]);
memset(current, 0, sizeof(char) * (string_length + 1));
} else {
character_append(current, string[index]);
}
}
character_append(result, current[0]);
free(current);
return result;
}

View File

@ -0,0 +1,31 @@
#ifndef STRING_H
#define STRING_H
/**
* @brief Converts all the alphabetic characters in a string to uppercase.
*
* @param 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 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);
/**
* @brief Converts a string to its acronym.
*
* @param string
* @return char*
*/
char* string_acronym(char* string);
#endif