Daily Archives: December 5, 2020
C++ || How To Check If A String Is Empty Or Only Contains Whitespace Using C++
The following is a module with functions which demonstrates how to determine whether a string is empty, or consists only of white-space characters using C++.
1. Is Null Or Whitespace
The example below demonstrates the use of ‘Utils::isNullOrWhitespace‘ to determine whether a string is empty, or consists only of white-space characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Is Null Or Whitespace // Declare values std::string words[] = { "Kenneth", "", "Jennifer", " ", "Lynn", "\n\t", "Sole" }; // Check to see which is empty for (auto& word: words) { bool isEmpty = Utils::isNullOrWhitespace(word); std::cout << "Word: " << word << ", Is Empty: " << (isEmpty ? "True" : "False") << std::endl; } // expected output: /* Word: Kenneth, Is Empty: False Word: , Is Empty: True Word: Jennifer, Is Empty: False Word: , Is Empty: True Word: Lynn, Is Empty: False Word: , Is Empty: True Word: Sole, Is Empty: False */ |
2. 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 |
// ============================================================================ // 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: isNullOrWhitespace * USE: Indicates whether a specified string is empty, or consists only * of whitespace characters * @param str: The source string * @return: True if str is Empty, or consists of only whitespace characters, * False otherwise */ bool isNullOrWhitespace(const std::string& str) { return str.empty() || std::all_of(str.begin(), str.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); }); } }// http://programmingnotes.org/ |
3. 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 |
// ============================================================================ // 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 values std::string words[] = { "Kenneth", "", "Jennifer", " ", "Lynn", "\n\t", "Sole" }; // Check to see which is empty for (auto& word: words) { bool isEmpty = Utils::isNullOrWhitespace(word); display("Word: " + word + ", Is Empty: " + (isEmpty ? "True" : "False")); } } 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.
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.
C++ || How To Remove All Whitespace From A String Using C++
The following is a module with functions which demonstrates how to remove all whitespace from a string using C++.
1. Remove All Whitespace
The example below demonstrates the use of ‘Utils::RemoveWhitespace‘ to remove all whitespace from a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Remove All Whitespace // Declare word std::string word = "This document uses 3 other documents "; // Remove whitespace from the string auto result = Utils::removeAllWhitespace(word); std::cout << result; /* expected output: Thisdocumentuses3otherdocuments */ |
2. 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 |
// ============================================================================ // 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: removeAllWhitespace * USE: Removes all whitespace from a string * @param source: The source string * @return: The modified source string */ std::string removeAllWhitespace(std::string source) { source.erase(std::remove_if(source.begin(), source.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); }), source.end()); return source; } }// http://programmingnotes.org/ |
3. 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 |
// ============================================================================ // 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 = "This document uses 3 other documents "; // Remove whitespace from the string auto result = Utils::removeAllWhitespace(word); display(result); } 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.
C++ || How To Get The File Path, File Name & File Extension From A Path Using C++
The following is a module with functions which demonstrates how to parse a file path, file name and file extension from a path using C++.
1. Get File Path
The example below demonstrates the use of ‘Utils::getFilePath‘ to parse and get the file path from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Get File Path // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file path auto filePath = Utils::getFilePath(path); std::cout << "Path: " << path << std::endl; std::cout << "File Path: " << filePath; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Path: C:\Users\Name\Desktop */ |
2. Get File Name
The example below demonstrates the use of ‘Utils::getFileName‘ to parse and get the file name from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Get File Name // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file name auto fileName = Utils::getFileName(path); std::cout << "Path: " << path << std::endl; std::cout << "File Name: " << fileName; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Name: 20171216_155433.jpg */ |
3. Get File Name Extension
The example below demonstrates the use of ‘Utils::getFileExtension‘ to parse and get the file extension from a path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Get File Name Extension // Declare path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file extension auto extension = Utils::getFileExtension(path); std::cout << "Path: " << path << std::endl; std::cout << "File Extension: " << extension; // expected output: /* Path: C:\Users\Name\Desktop\20171216_155433.jpg File Extension: jpg */ |
4. 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// ============================================================================ // 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 <algorithm> #include <cctype> namespace Utils { /** * FUNCTION: getFilePath * USE: Returns the path from a given file path * @param path: The path of the file * @return: The path from the given file path */ std::string getFilePath(const std::string& path) { auto pathEnd = path.find_last_of("/\\"); auto pathName = pathEnd == std::string::npos ? path : path.substr(0, pathEnd); return pathName; } /** * FUNCTION: getFileName * USE: Returns the file name from a given file path * @param path: The path of the file * @return: The file name from the given file path */ std::string getFileName(const std::string& path) { auto fileNameStart = path.find_last_of("/\\"); auto fileName = fileNameStart == std::string::npos ? path : path.substr(fileNameStart + 1); return fileName; } /** * FUNCTION: getFileExtension * USE: Returns the file extension from a given file path * @param path: The path of the file * @return: The file extension from the given file path */ std::string getFileExtension(const std::string& path) { auto fileName = getFileName(path); auto extStart = fileName.find_last_of('.'); auto ext = extStart == std::string::npos ? "" : fileName.substr(extStart + 1); std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return static_cast<unsigned char>(std::tolower(c)); }); return ext; } }// http://programmingnotes.org/ |
5. 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 40 41 42 43 44 45 |
// ============================================================================ // 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 path std::string path = "C:\\Users\\Name\\Desktop\\20171216_155433.jpg"; // Get the file path auto filePath = Utils::getFilePath(path); display("Path: " + path + ", File Path: " + filePath); // Get the filename auto fileName = Utils::getFileName(path); display("Path: " + path + ", File Name: " + fileName); // Get the file extension auto extension = Utils::getFileExtension(path); display("Path: " + path + ", File Extension: " + extension); } 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.