Sha256: 9b4481bc0196d8771926f65c31bb88bb0e44b30ee80d3b8051dfaa2cc4094081

Contents?: true

Size: 1.17 KB

Versions: 193

Compression:

Stored size: 1.17 KB

Contents

#include <ctype.h>
#include <stdlib.h>

static int is_first_letter_of_word(int character, int preceding_character)
{
   return isalpha(character)
       && ((preceding_character == ' ')
           || (preceding_character == '-')) ? 1 : 0;
}

static int count_words(const char phrase[])
{
   int count = 0;

   if (phrase != NULL)
      count = 1;                /* first word */
   else
      return count;

   for (size_t i = 1; phrase[i] != '\0'; ++i)
      if (is_first_letter_of_word(phrase[i], phrase[i - 1]))
         ++count;               /* subsequent words */
   return count;
}

char *abbreviate(const char phrase[])
{
   char *acronym = NULL;

   if (phrase == NULL || phrase[0] == '\0')
      return NULL;              /* invalid argument */

   acronym = calloc(count_words(phrase) + 1, sizeof phrase[0]);
   if (!acronym)
      return NULL;              /* calloc failed */

   acronym[0] = toupper(phrase[0]);     /* first letter of first word */

   for (size_t i = 1, j = 1; phrase[i] != '\0'; ++i) {
      if (is_first_letter_of_word(phrase[i], phrase[i - 1]))
         acronym[j++] = toupper(phrase[i]);     /* first letter of subsequent words */
   }
   return acronym;
}

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.179 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.178 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.177 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.176 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.175 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.174 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.173 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.172 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.171 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.170 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.169 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.167 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.166 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.165 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.164 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.163 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.162 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.161 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.160 tracks/c/exercises/acronym/src/example.c