In this article we will discuss different ways to convert first letter of each word to upper case in a a given string i.e.
- Using STL algorithm
- Using Boost Filtered Iterator & Range
Converting First Letter to Upper Case using STL
Algorithm Used:
- Iterate over the characters in given string
- For each character check – if last char was ‘ ‘ and current is not ‘ ‘
- If yes the convert that letter to upper case
Complete code is as follows,
#include <iostream> #include <string> #include <algorithm> int main() { std::string data = "hi this is a sample string."; // Iterate over all the characters in a string // and call a lambda function on each of them std::for_each(data.begin(), data.end(), [](char & c) { static int last = ' '; if(last == ' ' && c != ' ' && ::isalpha(c)) c = ::toupper(c); last = c; }); std::cout << data << std::endl; }
Output:
Hi This Is A Sample String.
Converting First Letter to Upper Case using Boost Library
Boost Library provides a function to convert a input sequence to upper case i.e.
template<typename WritableRangeT> void to_upper(WritableRangeT & Input, const std::locale & Loc = std::locale());
Here input sequenece can be a string or a range of iterators i.e. boost::iterator_range
But here we don’t want to convert whole string to upper case. We want few specific characters to be converted to upper case only. Therefore we will create a Range of Filtered Iterator.
Filtered Iterator
Filtered Iterator is an adaptor iterator that will skip certain elements based based on filter i.e. an attached callback.
Let’s first create a callback that will accept a char and return true if last character received was whitespace ‘ ‘ and current char is an alphabet.
struct IsFirstLetter { // Return true if last char was ' ' & current char is an alphabet. bool operator()(char & c) { static int last = ' '; if (last == ' ' && c != ' ' && ::isalpha(c)) { last = c; return true; } last = c; return false; } };
Now let’s create Start and End Filtered String Iterator that will iterate through only those characters which when passed to above filter callback returns true i.e.
std::string data = "hi this is a sample string."; typedef boost::filter_iterator<IsFirstLetter, std::string::iterator> FilterIter; // Creating Filter i.e. a Functor Object IsFirstLetter isFirstLetterObj; // Create start of Filtered Iterator FilterIter filter_iter_first(isFirstLetterObj, data.begin(), data.end()); // Create End of Filtered Iterator FilterIter filter_iter_last(isFirstLetterObj, data.end(), data.end());
Now create a Range from this start and end filtered iterator i.e.
// Create a Range from start & end filetered iterator object boost::iterator_range<FilterIter> strRange; strRange = boost::make_iterator_range(filter_iter_first, filter_iter_last);
Now use this range with boost::to_upper to convert only those characters to uppercase which are in filtered iterator i.e.
// convert string to upper case boost::to_upper(strRange);
Complete example is as follows,
#include <iostream> #include <string> #include <algorithm> #include <boost/algorithm/string.hpp> #include <boost/iterator/filter_iterator.hpp> struct IsFirstLetter { // Return true if last char was ' ' & current char is an alphabet. bool operator()(char & c) { static int last = ' '; if (last == ' ' && c != ' ' && ::isalpha(c)) { last = c; return true; } last = c; return false; } }; int main() { std::string data = "hi this is a sample string."; typedef boost::filter_iterator<IsFirstLetter, std::string::iterator> FilterIter; // Creating Filter i.e. a Functor Object IsFirstLetter isFirstLetterObj; // Create start of Filtered Iterator FilterIter filter_iter_first(isFirstLetterObj, data.begin(), data.end()); // Create End of Filtered Iterator FilterIter filter_iter_last(isFirstLetterObj, data.end(), data.end()); // Create a Range from start & end filetered iterator object boost::iterator_range<FilterIter> strRange; strRange = boost::make_iterator_range(filter_iter_first, filter_iter_last); // convert string to upper case boost::to_upper(strRange); std::cout << data << std::endl; }
Output:
Hi This Is A Sample String.