If you have an array with element type std::string
like this
std::string word[] = { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };
then you can convert all its elements for example to the lower case the following way using the range-based for loop
#include <string>
#include <cctype>
//…
for ( auto &s : word )
{
for ( char &c : s ) c = tolower( ( unsigned char )c );
}
Here is a demonstrative program.
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string word[] = { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };
for ( const auto &s : word ) std::cout << s << ' ';
std::cout << '\n';
for ( auto &s : word )
{
for ( char &c : s ) c = std::tolower( ( unsigned char )c );
}
for ( const auto &s : word ) std::cout << s << ' ';
std::cout << '\n';
for ( auto &c : word[0] ) c = std::toupper( ( unsigned char )c );
for ( const auto &s : word ) std::cout << s << ' ';
std::cout << '\n';
}
Its output is
VERY MERRY CHRISTMAS EVERYONE
very merry christmas everyone
VERY merry christmas everyone
Or you can use a user-defined function like this.
#include <iostream>
#include <string>
#include <cctype>
std::string & change_string_case( std::string &s, bool upper_case = true )
{
for ( auto &c : s ) c = upper_case ? std::toupper( static_cast<unsigned char>( c ) )
: std::tolower( static_cast<unsigned char>( c ) );
return s;
}
int main()
{
std::string word[] = { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };
for ( auto &s : word ) std::cout << change_string_case( s, false ) << ' ';
std::cout << '\n';
}
The program output is
very merry christmas everyone
Or you can write two separate functions as for example
#include <iostream>
#include <string>
#include <cctype>
std::string & lowercase_string( std::string &s )
{
for ( auto &c : s ) c = std::tolower( static_cast<unsigned char>( c ) );
return s;
}
std::string & uppercase_string( std::string &s )
{
for ( auto &c : s ) c = std::toupper( static_cast<unsigned char>( c ) );
return s;
}
int main()
{
std::string word[] = { "VERY", "MERRY", "CHRISTMAS", "EVERYONE" };
for ( auto &s : word ) std::cout << lowercase_string( s ) << ' ';
std::cout << '\n';
for ( auto &s : word ) std::cout << uppercase_string( s ) << ' ';
std::cout << '\n';
}
The program output is
very merry christmas everyone
VERY MERRY CHRISTMAS EVERYONE