nt_string_c is a custom implementation of a subset of the standard C string functions (libc-like behavior, no safety checks for NULL).
| Project | nt_string_c |
|---|---|
| Open | 2026-01-23 03:02 CEST (UTC+1) |
| Closed | Not closed yet |
nt_strlen(const char *s)Returns the length of the strings(excluding the terminating'\0').nt_strdup(const char *s)Allocates and returns a newly created copy ofs(caller mustfree()it).
nt_strcmp(const char *s1, const char *s2)Lexicographically comparess1ands2. Returns< 0,0, or> 0.nt_strncmp(const char *s1, const char *s2, size_t n)Same asnt_strcmpbut compares at mostncharacters.
nt_strchr(const char *s, int c)Returns a pointer to the first occurrence ofcins, orNULLif not found. Ifc == '\0', returns a pointer to the terminating'\0'.nt_strrchr(const char *s, int c)Returns a pointer to the last occurrence ofcins, orNULLif not found. Ifc == '\0', returns a pointer to the terminating'\0'.nt_strstr(const char *haystack, const char *needle)Returns a pointer to the first occurrence ofneedleinhaystack, orNULL. Ifneedleis empty, returnshaystack.
nt_memset(void *s, int c, size_t n)Fills the firstnbytes ofswith byte valuec. Returnss.nt_memcpy(void *dest, const void *src, size_t n)Copiesnbytes fromsrctodest. Returnsdest(requires non-overlapping ranges).nt_memmove(void *dest, const void *src, size_t n)Copiesnbytes fromsrctodestsafely when ranges overlap. Returnsdest.
- These functions assume valid pointers: passing
NULLresults in undefined behavior (like libc). - Returned pointers from
nt_strchr,nt_strrchr, andnt_strstrpoint inside the original string (no allocation). nt_strdupallocates memory usingmalloc: you mustfree()the returned pointer.nt_memcpyis undefined for overlapping ranges; usent_memmovefor overlap-safe copying.
This project builds a static library:
makeOutput:
- Library:
build/lib/libnt_string_c.a - Objects:
build/obj/*.o
Cleaning:
make clean # removes objects + dependency files
make fclean # removes build directory
make re # rebuildExample:
#include "nt_string.h"
int main(void)
{
const char *s = "hello";
char *dup = nt_strdup(s);
/* ... use dup ... */
free(dup);
return 0;
}Compile with:
gcc -I./include main.c -L./build/lib -lnt_string_c.
├── include/
│ └── nt_string.h
├── src/
│ ├── nt_strlen.c
│ ├── nt_strdup.c
│ ├── nt_strcmp.c
│ ├── nt_strncmp.c
│ ├── nt_strchr.c
│ ├── nt_strrchr.c
│ ├── nt_strstr.c
│ ├── nt_memset.c
│ ├── nt_memcpy.c
│ └── nt_memmove
└── Makefile
- C standard: C89
- Recommended flags:
-Wall -Wextra -Werror -pedantic -std=c89
- Continue to remake the string library functions for learning purposes
This project is under BSD 3-Clause License