The strTools
namespace provides a set of tools for manipulating C-style strings. These functions are designed to simplify common string operations, such as concatenation, substring extraction, insertion, deletion, searching, and replacement. The library ensures proper memory management using unique_ptr<char[]>
.
This project provides a string manipulation library to easily handle strings. It includes functions for calculating the length of a string, concatenating strings, searching for a substring, and generating substrings. The main.cpp
file demonstrates examples of how to use this library through a simple console menu.
- Length Calculation: Calculate the length of a C-string.
- Concatenation: Concatenate two C-strings into a new dynamically allocated string.
- Substring Extraction: Extract a substring from a C-string.
- Insertion: Insert one C-string into another at a specified position.
- Deletion: Remove a substring from a C-string.
- Searching: Find the first occurrence of a substring within a C-string.
- Replacement: Replace the first occurrence of a substring with another substring.
- Calculate the Length of a String: Enter a string and get its length.
- Concatenate Strings: Enter three strings and concatenate them.
- Search for a Substring: Enter a string and a substring to find its position within the string.
- Generate a Substring: Enter a string and generate a random substring from it.
- Exit: Exit the program.
To use the strTools
& strUtil
library, include the strtools.hh
header in your C++ project:
#include "src/.hxx"
or:
#include "src/strlogger.hh"
#include "src/strtools.hh"
#include "src/strutil.hh"
#include "src/strutilhelper.hh"
Ensure that your project is set up to find the header file in its include path.
You can also try running the test program using:
g++ -std=c++17 -I src main.cpp -o main.exe && ./main.exe
Calculate the length of a C-string.
const char* myString = "Hello, World!";
uint64_t length = strTools::len(myString); // length will be 13
Concatenate two C-strings into a new unique_ptr<char[]>.
const char* str1 = "Hello, ";
const char* str2 = "World!";
auto result = strTools::concatStr(str1, str2);
// result will contain "Hello, World!"
Extract a substring from a C-string.
const char* myString = "Hello, World!";
auto sub = strTools::subStr(myString, 7, 5);
// sub will contain "World"
Insert one C-string into another at a specified position.
const char* str1 = "Hello, World!";
const char* str2 = "Beautiful ";
auto result = strTools::insertStr(str1, str2, 8);
// result will contain "Hello, Beautiful World!"
Remove a substring from a C-string.
const char* myString = "Hello, World!";
auto result = strTools::delSubStr(myString, 7, 6);
// result will contain "Hello, !"
Find the first occurrence of a substring within a C-string.
const char* myString = "Hello, World!";
int64_t index = strTools::findSubStr(myString, "World");
// index will be 7
Replace the first occurrence of a substring with another substring.
const char* myString = "Hello, World!";
const char* sub1 = "World";
const char* sub2 = "Universe";
auto result = strTools::replaceStr(myString, sub1, sub2);
// result will contain "Hello, Universe!"
NOTE: The main
function requires C++20. If you are using C++17, this section will not compile.
-
Calculate the Length of a String:
- Prompts the user to enter a string.
- Calculates the length using
strTools::len
.
-
Concatenate Three Strings:
- Prompts the user to enter three strings.
- Concatenates them using
strTools::concatStr
. - Displays the concatenated result.
-
Search for a Substring:
- Prompts the user to enter a string and a substring.
- Searches for the substring using
strTools::findSubStr
. - Extracts the substring using
strTools::subStr
. - Displays the result or an error message if the substring is not found.
-
Generate a Substring from a String:
- Prompts the user to enter a string.
- Generates random start and end indices.
- Extracts a substring using
strTools::subStr
. - Displays the extracted substring.
-
Exit:
- Exits the program.
- Run the program.
- Select an option by entering a number (0-4).
- Follow the prompts to perform the desired operation.
- View the result or error message.
- Repeat until you choose to exit (option 0).
The program uses the helpers
namespace to manage invalid inputs, out-of-bounds values, and user input for different operations:
- Invalid Input: If the input is invalid (non-numeric), an error message is shown.
- Out-of-Bounds Input: If the input is not within the range [0, 4], an error message is shown.
- User Input Handling: Manages input from the user, including handling exit commands and input overflow.
The strtools
namespace provides the following functions for string manipulation:
- Length Calculation (
strTools::len
): Calculates the length of a string. - Concatenation (
strTools::concatStr
): Concatenates multiple strings. - Substring Search (
strTools::findSubStr
): Finds the position of a substring within a string. - Substring Extraction (
strTools::subStr
): Extracts a substring from a string based on start and end indices.
For more detailed documentation on the code, including function descriptions and usage, refer to the Doxygen documentation available here.
The strTools
library is licensed under the GNU General Public License v3.0. For more details, see license.