Design Pattern Examples
Overview of object-oriented design patterns
titlecase.cpp
Go to the documentation of this file.
1
5
6#include <sstream>
7#include "titlecase.h"
8
9namespace Helpers
10{
11 std::string titlecase(const std::string& s)
12 {
13 std::ostringstream output;
14
15 for (size_t index = 0; index < s.size(); index++)
16 {
17 int c = s[index];
18 // Stop on space or non-alphabetic character (presumably punctuation)
19 if (std::isspace(c) || !std::isalpha(c))
20 {
21 // Reached end of a word, copy rest of string over
22 output << s.substr(index);
23 // And we are done
24 break;
25 }
26
27 if (index == 0)
28 {
29 c = std::toupper(c);
30 }
31 else
32 {
33 c = std::tolower(c);
34 }
35 output << static_cast<char>(c);
36 }
37
38 return output.str();
39 }
40
41} // end namespace
Declaration of the titlecase() function, for making a word lowercase with the first letter uppercase.
The namespace containing all the "helper" functions in the C++ code.
std::string titlecase(const std::string &s)
Convert the first word (or only word) in the given string to lowercase then make the first letter upp...
Definition: titlecase.cpp:11