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,

Output:

 

Converting First Letter to Upper Case using Boost Library

Boost Library provides a function to convert a input sequence to upper case i.e.

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.

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.

Now create a Range from this start and end filtered iterator i.e.

Now use this range with boost::to_upper to convert only those characters to uppercase which are in filtered iterator i.e.

Complete example is as follows,

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.