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

feat(solutions): add look-and-say-sequence-conway/c/function

This commit is contained in:
Divlo 2021-11-30 16:42:54 +01:00
parent b3fb594865
commit 6d30c3defe
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
7 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,3 @@
# look-and-say-sequence-conway/c/function
Created by [@Divlo](https://github.com/Divlo) on 30 November 2021.

View File

@ -0,0 +1,17 @@
#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';
}
void character_append_many(char* string, char* characters) {
size_t characters_length = strlen(characters);
for (size_t index = 0; index < characters_length; index++) {
character_append(string, characters[index]);
}
}

View File

@ -0,0 +1,22 @@
#ifndef __CHARACTER__
#define __CHARACTER__
/**
* @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 Append many characters to a string, assuming string points to an array
* with enough space.
*
* @param string
* @param characters
*/
void character_append_many(char* string, char* characters);
#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__
#define __INPUT__
/**
* @brief Read a line from stdin.
*
* @return char*
*/
char* input();
#endif

View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "character.h"
#include "input.h"
int main() {
char* string = input();
size_t string_length = strlen(string);
char* result = malloc(sizeof(char*) * (string_length + 1));
for (size_t index = 0; index < string_length; index++) {
size_t number_of_appearances = 0;
char value_to_search = string[index];
size_t iteration = index;
while (iteration < string_length && string[iteration] == value_to_search) {
number_of_appearances++;
iteration++;
}
char* number_of_appearances_string = malloc(sizeof(char*) * (string_length + 1));
snprintf(number_of_appearances_string, sizeof(result), "%zu", number_of_appearances);
character_append_many(result, number_of_appearances_string);
character_append(result, value_to_search);
index += number_of_appearances - 1;
}
printf("%s\n", result);
return EXIT_SUCCESS;
}

View File

@ -18,7 +18,7 @@ namespace Solution
numberOfAppearances += 1;
iteration++;
}
result = result + numberOfAppearances.ToString() + line[index];
result = result + numberOfAppearances.ToString() + valueToSearch;
index += numberOfAppearances - 1;
}
Console.WriteLine(result);