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.119 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.118 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.117 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.116 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.115 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.114 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.113 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.111 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.110 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.109 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.108 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.107 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.106 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.105 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.104 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.103 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.102 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.101 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.100 tracks/c/exercises/acronym/src/example.c
trackler-2.2.1.99 tracks/c/exercises/acronym/src/example.c