Design Pattern Examples
Overview of object-oriented design patterns
titlecase.c
Go to the documentation of this file.
1
5
6#include <ctype.h>
7#include <stdint.h>
8#include <stdlib.h>
9#include <stdio.h>
10#include <memory.h>
11#include <string.h>
12
13#include "titlecase.h"
14
15char* titlecase(const char* s)
16{
17 char* buffer = NULL;
18
19 if (s != NULL)
20 {
21 size_t stringSize = strlen(s);
22 buffer = calloc(1, stringSize + 1);
23 if (buffer != NULL)
24 {
25 for (size_t index = 0; index < stringSize; index++)
26 {
27 int c = s[index];
28 // Stop on space or non-alphabetic character (presumably punctuation)
29 if (isspace(c) || !isalpha(c))
30 {
31 // Reached end of a word, copy rest of string over
32 // Note: buffer was initialized with 0's and we allocated
33 // for enough space to hold string plus null terminator.
34 strcat(buffer, s + index);
35 // And we are done
36 break;
37 }
38
39 if (index == 0)
40 {
41 c = toupper(c);
42 }
43 else
44 {
45 c = tolower(c);
46 }
47 buffer[index] = (char)c;
48 }
49 }
50 }
51
52 return buffer;
53}
Declaration of the titlecase() function, for making a word lowercase with the first letter uppercase.
char * titlecase(const char *s)
Convert the first word (or only word) in the given string to lowercase then make the first letter upp...
Definition: titlecase.c:15