C++ || How To Convert A String To Upper and Lower Case Using C++
The following is a module with functions which demonstrates how to convert a string to all upper and lower case using C++.
1. To Upper Case
The example below demonstrates the use of ‘Utils::toUpperCase‘ to convert a string to all upper case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// To Upper Case // Declare word std::string word = "Kenneth, Jennifer, Lynn, Sole"; // Convert to uppercase auto result = Utils::toUpperCase(word); std::cout << result; // expected output: /* KENNETH, JENNIFER, LYNN, SOLE */ |
2. To Lower Case
The example below demonstrates the use of ‘Utils::toLowerCase‘ to convert a string to all lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// To Lower Case // Declare word std::string word = "Kenneth, Jennifer, Lynn, Sole"; // Convert to lowercase auto result = Utils::toLowerCase(word); std::cout << result; // expected output: /* kenneth, jennifer, lynn, sole */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 5, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <string> #include <cctype> #include <algorithm> namespace Utils { /** * FUNCTION: toUpperCase * USE: Converts a string to all uppercase * @param source: The source string * @return: The modified source string */ std::string toUpperCase(std::string source) { std::transform(source.begin(), source.end(), source.begin(), [](unsigned char c) { return static_cast<unsigned char>(std::toupper(c)); }); return source; } /** * FUNCTION: toLowerCase * USE: Converts a string to all lowercase * @param source: The source string * @return: The modified source string */ std::string toLowerCase(std::string source) { std::transform(source.begin(), source.end(), source.begin(), [](unsigned char c) { return static_cast<unsigned char>(std::tolower(c)); }); return source; } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// ============================================================================ // Author: Kenneth Perkins // Date: Dec 5, 2020 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { // Declare word std::string word = "Kenneth, Jennifer, Lynn, Sole"; // Convert to uppercase auto result = Utils::toUpperCase(word); display(result); // Convert to lowercase auto result2 = Utils::toLowerCase(word); display(result2); } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply