From e4fbd8bbfb6ddac8e608cb7ce9b9bc65781fd729 Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Wed, 25 Apr 2018 15:29:24 +0200 Subject: [PATCH] syllable.c: add syllable_chain_to_string() --- syllable.c | 29 +++++++++++++++++++++++++++++ syllable.h | 3 +++ 2 files changed, 32 insertions(+) diff --git a/syllable.c b/syllable.c index bdd619c..bf41768 100644 --- a/syllable.c +++ b/syllable.c @@ -30,3 +30,32 @@ struct syllable *syllable_append(struct syllable *tail, const char *data) return ptr; } + +unsigned long syllable_chain_length(struct syllable *head) +{ + struct syllable *walk = head; + unsigned int length = 0; + + while (walk) { + length += strlen(walk->data); + walk = walk->next; + } + + return length; +} + +char *syllable_chain_to_string(struct syllable *head) +{ + struct syllable *walk = head; + unsigned int length = syllable_chain_length(head); + char *buffer = malloc(length + 1); + char *ptr = buffer; + + while (walk) { + strcpy(ptr, walk->data); + ptr += strlen(walk->data); + walk = walk->next; + } + + return buffer; +} diff --git a/syllable.h b/syllable.h index 7c2b426..c4a8bec 100644 --- a/syllable.h +++ b/syllable.h @@ -11,4 +11,7 @@ void syllable_drop(struct syllable *syllable); struct syllable *syllable_append(struct syllable *tail, const char *data); +unsigned long syllable_chain_length(struct syllable *head); +char *syllable_chain_to_string(struct syllable *head); + #endif /* __SYLLABE_H */