Strings:
C++ capitalize first letter of string: The declaration and description of a string using an array of chars are the same as the declaration and definition of any other data type array.
Any string must end with the null character ‘/0′. As the last element of an array specified in this manner, the null character ‘/0′ should be used.
Given a string ,the task is to convert the first letter of each word in the given string to uppercase
Example:
Example 1:
Input:
given_string="hello this is btechgeeks"
Output:
Hello This Is BTechgeeks
Example 2:
Input:
given_string="miss World"
Output:
Miss World
Explanation :
Here second word "world" is already capitalized
Convert First Letter of Each word in a String to Uppercase
There are several ways to convert the first letter of each word in a string to uppercase some of them are:
Method #1:Using STL functions
Approach/Algorithm:
- Iterate through the characters in the specified string.
- Check for each character – if the previous character was ‘ ‘ and the current character is not ‘ ‘
- If yes, change the letter to upper case.
Below is the implementation:
#include <bits/stdc++.h>
using namespace std;
int main()
{
// given string
string given_string = "hello this is btechgeeks";
// Traverse the givn_string
for (int i = 0; i < given_string.length(); i++) {
static int last = ' ';
if (last == ' ' && given_string[i] != ' '
&& ::isalpha(given_string[i]))
given_string[i] = ::toupper(given_string[i]);
last = given_string[i];
}
// printing the string
cout << given_string << endl;
}
Output:
Hello This Is Btechgeeks
Method #2:Using Boost Library
A feature in the Boost Library converts an input sequence to upper case.
template<typename WritableRangeT> void to_upper(WritableRangeT & Input, const std::locale & Loc = std::locale());
In this case, the input sequence may be a string or a range of iterators, such as boost::iterator_range.
However, we do not want to move the entire string to upper case. We only want a few basic characters translated to upper case. As a result, we’ll make a Range of Filtered Iterator.
Filtered Iterator:
Filtered Iterator is an adaptor iterator that skips certain elements based on a filter, such as a callback attached to it.
Approach:
- Build a callback that will accept a char and return true if the previous character was whitespace ‘ ‘ and the current char is an alphabet.
- Build a Start and End Filtered String Iterator that iterates through only the characters that return true when passed to the above filter callback
- Make a Range out of this filtered iterator’s start and end points.
- Use this range in conjunction with boost::to_upper to convert only the characters in the filtered iterator to uppercase.
Below is the implementation:
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
#include <boost/iterator/filter_iterator.hpp>
using namespace std;
struct isFirstCharacter {
// If the previous char was ' ' and the current char is
// an alphabet, return true.
bool operator()(char& character)
{
static int lastchar = ' ';
if (lastchar == ' ' && character != ' '
&& ::isalpha(character)) {
lastchar = character;
return true;
}
lastchar = character;
return false;
}
};
int main()
{
// given string
string given_string = "hello this is btechgeeks";
typedef boost::filter_iterator<isFirstCharacter,
string::iterator>
filter_iterator;
// Creating a Filter, also known as a Functor Object
isFirstCharacter isFirstLetterObject;
// Create the Filtered Iterator's beginning.
filter_iterator first_filter_itr(isFirstLetterObject,
given_string.begin(),
given_string.end());
// Create the Filtered Iterator's End
filter_iterator last_filter_itr(isFirstLetterObject,
given_string.end(),
given_string.end());
// Create a Range from a filetered iterator's start and
// end points object
boost::iterator_range<filter_iterator> stringRange;
stringRange = boost::make_iterator_range(
first_filter_itr, last_filter_itr);
// convert string to upper case
boost::to_upper(stringRange);
// print the string
cout << given_string << endl;
}
Output:
Hello This Is Btechgeeks
Related Programs:
- python capitalize the first letter of each word in a string
- capitalize first letter of each word in a string in java
- how to uppercase the first letter of a string in javascript
- converting a string to upper lower case in cpp using stl boost library
- python program to read a file and capitalize the first letter of every word in the file
- find all occurrences of a sub string in a string cpp both case sensitive insensitive
- python how to get first n characters in a string