A comprehensive C library implementing string manipulation functions with custom implementations of sprintf and sscanf.
StringPlus is a feature-rich string processing library written in C11. It provides a complete reimplementation of the standard string.h library functions, along with custom implementations of sprintf and sscanf from stdio.h, plus additional utility functions inspired by C#'s String class.
Complete implementation of standard C string manipulation functions:
- Memory operations:
memchr,memcmp,memcpy,memset - String operations:
strlen,strchr,strrchr,strcmp,strcpy,strcat,strncat,strncpy,strncmp - Search functions:
strstr,strpbrk,strcspn,strspn,strtok - Error handling:
strerrorwith platform-specific error messages
-
sprintf: Full implementation supporting all standard format specifiers
- Specifiers:
c,d,i,f,e,E,g,G,s,u,x,X,o,p,n,% - Flags:
-,+,(space),#,0 - Width and precision modifiers
- Length modifiers:
h,l,L
- Specifiers:
-
sscanf: Complete implementation for parsing formatted input
- All standard format specifiers
- Width and length modifiers
- Suppression flag
*
Inspired by C# String class:
to_upper: Convert string to uppercaseto_lower: Convert string to lowercaseinsert: Insert substring at specified positiontrim: Remove leading and trailing characters
- Language: C11 standard
- Compiler: GCC
- Code Style: Google C++ Style Guide adapted for C
- Testing: Unit tests with Check library
- Test Coverage: 80%+ for all functions
- Platform Support: Cross-platform (Linux, macOS)
# Build the library
make
# Run tests
make test
# Generate coverage report
make gcov_report
# Clean build artifacts
make cleanStringPlus/
├── .gitignore
├── LICENSE
├── README.md
└── src/
├── Makefile
├── string_plus.h # Main header file
├── plus_sprintf.h # sprintf header
│
├── plus_memchr.c # Memory functions
├── plus_memcmp.c
├── plus_memcpy.c
├── plus_memset.c
│
├── plus_strlen.c # String functions
├── plus_strchr.c
├── plus_strrchr.c
├── plus_strcmp.c
├── plus_strncmp.c
├── plus_strcpy.c
├── plus_strncpy.c
├── plus_strcat.c
├── plus_strncat.c
├── plus_strcspn.c
├── plus_strspn.c
├── plus_strpbrk.c
├── plus_strstr.c
├── plus_strtok.c
├── plus_strerror.c
│
├── plus_sprintf.c # Formatted output
├── plus_sscanf.c # Formatted input
│
├── plus_to_upper.c # Utility functions
├── plus_to_lower.c
├── plus_insert.c
├── plus_trim.c
│
├── plus_assign_char_value.c # sscanf helpers
├── plus_assign_float_value.c
├── plus_assign_integer_value.c
├── plus_assign_pointer_value.c
├── plus_assign_string_value.c
├── plus_char_to_digit.c
├── plus_handle_literal_match.c
├── plus_parse_exponent.c
├── plus_parse_float.c
├── plus_parse_format_spec.c
├── plus_parse_fraction_part.c
├── plus_parse_integer.c
├── plus_parse_integer_part.c
├── plus_parse_sign.c
├── plus_process_percent.c
├── plus_process_specifier.c
├── plus_scan_char.c
├── plus_scan_float.c
├── plus_scan_integer.c
├── plus_scan_pointer.c
├── plus_scan_string.c
├── plus_skip_whitespace.c
│
├── test_string_plus.c # Unit tests
└── utils.c # Utility functions
All functions implement proper bounds checking and handle edge cases to prevent buffer overflows and undefined behavior.
Error message handling (strerror) includes platform-specific implementations for both macOS and Linux systems.
The sprintf implementation matches the standard library's output precision (up to 1e-6 for floating-point numbers).
Unit tests compare implementation results against the standard library to ensure correctness and compatibility.
- Low-level C programming: Direct memory manipulation, pointer arithmetic
- Standard library implementation: Deep understanding of C standard library internals
- Parsing and formatting: Complex format string parsing and number formatting algorithms
- Cross-platform development: Platform-specific code with preprocessor directives
- Testing and quality assurance: Comprehensive unit testing with high code coverage
- Software engineering practices: Clean code, modular design, documentation
#include "string_plus.h"
int main() {
// Using standard string functions with plus_ prefix
char str[100] = "Hello, ";
plus_strncat(str, "World!", 10);
printf("Length: %zu\n", plus_strlen(str));
// Using sprintf implementation
char buffer[100];
plus_sprintf(buffer, "Number: %d, Float: %.2f", 42, 3.14);
// Using sscanf implementation
int num;
float flt;
plus_sscanf("42 3.14", "%d %f", &num, &flt);
// Using utility functions
char *upper = plus_to_upper("hello");
char *trimmed = plus_trim(" spaces ", " ");
free(upper);
free(trimmed);
return 0;
}All functions use the plus_ prefix to avoid naming conflicts with standard library functions.
plus_size_t plus_strlen(const char *str)- Calculate string lengthchar *plus_strchr(const char *str, int c)- Find first occurrence of characterchar *plus_strstr(const char *haystack, const char *needle)- Find substringchar *plus_strncpy(char *dest, const char *src, plus_size_t n)- Copy string- And many more...
void *plus_memchr(const void *str, int c, plus_size_t n)- Find byte in memoryint plus_memcmp(const void *str1, const void *str2, plus_size_t n)- Compare memory blocksvoid *plus_memcpy(void *dest, const void *src, plus_size_t n)- Copy memoryvoid *plus_memset(void *str, int c, plus_size_t n)- Fill memory with byte
int plus_sprintf(char *str, const char *format, ...)- Format string outputint plus_sscanf(const char *str, const char *format, ...)- Parse formatted input
void *plus_to_upper(const char *str)- Convert to uppercase (allocates new string)void *plus_to_lower(const char *str)- Convert to lowercase (allocates new string)void *plus_insert(const char *src, const char *str, plus_size_t start_index)- Insert substringvoid *plus_trim(const char *src, const char *trim_chars)- Trim characters
The project includes comprehensive unit tests covering:
- Boundary conditions and edge cases
- Comparison with standard library behavior
- Memory safety and error handling
- Format string parsing accuracy
- Cross-platform compatibility
Run tests with:
cd src
make testGenerate coverage report:
cd src
make gcov_reportThis is a personal portfolio project demonstrating C programming skills. However, if you find bugs or have suggestions, feel free to open an issue.
MIT License - see LICENSE file for details.