I wrote a simple function that converts an array of words into proper case (using bitwise operations) and returns the array. I performed a simple test using std::chrono
to check how fast it was able to perform the conversion. Using a vector of 1000 words the function was able to do it in an average of 27 microseconds.
My question is, if there is a faster way of doing this or if there is anything that I have missed that may be detrimental to the performance of the function. In addition, if there is any syntactical issues or changes that you would recommend that would be greatly appreciated.
#include <vector>
#include <string>
std::vector<std::string> ToProperCase(std::vector<std::string>& array)
{
if (array.empty())
return array;
// Loop through each word
for (std::string& value : array)
{
// Loop through each character in the word
for (unsigned int i = 0; i < value.size(); ++i)
{
// Capitalise first character
value[0] &= (~(1 << 5));
// Convert character to lower case
if (i > 0)
value[i] |= (1 << 5);
}
}
return array;
}
toupper()
andtolower()
functions. I am mainly interested in the performance of using bit shifting directly as well as my coding methodologies such as if I should be checking if an array is empty or simply the naming conventions and style. I do 100% agree that it would be easier to use the functions mentioned above. \$\endgroup\$<<
used with a constant, so it doesn't count. I guess the right name is "bitwise" operations. \$\endgroup\$