Skip to content

A learning-focused reimplementation of the ANSI C string.h functions

License

Notifications You must be signed in to change notification settings

Ntalcme/nt_string_c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nt_string_c — Remake the string library

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

Features

String length / duplication

  • nt_strlen(const char *s) Returns the length of the string s (excluding the terminating '\0').
  • nt_strdup(const char *s) Allocates and returns a newly created copy of s (caller must free() it).

String comparison

  • nt_strcmp(const char *s1, const char *s2) Lexicographically compares s1 and s2. Returns < 0, 0, or > 0.
  • nt_strncmp(const char *s1, const char *s2, size_t n) Same as nt_strcmp but compares at most n characters.

Character / substring search

  • nt_strchr(const char *s, int c) Returns a pointer to the first occurrence of c in s, or NULL if not found. If c == '\0', returns a pointer to the terminating '\0'.
  • nt_strrchr(const char *s, int c) Returns a pointer to the last occurrence of c in s, or NULL if not found. If c == '\0', returns a pointer to the terminating '\0'.
  • nt_strstr(const char *haystack, const char *needle) Returns a pointer to the first occurrence of needle in haystack, or NULL. If needle is empty, returns haystack.

Memory operations

  • nt_memset(void *s, int c, size_t n) Fills the first n bytes of s with byte value c. Returns s.
  • nt_memcpy(void *dest, const void *src, size_t n) Copies n bytes from src to dest. Returns dest (requires non-overlapping ranges).
  • nt_memmove(void *dest, const void *src, size_t n) Copies n bytes from src to dest safely when ranges overlap. Returns dest.

libc-like behavior (important)

  • These functions assume valid pointers: passing NULL results in undefined behavior (like libc).
  • Returned pointers from nt_strchr, nt_strrchr, and nt_strstr point inside the original string (no allocation).
  • nt_strdup allocates memory using malloc: you must free() the returned pointer.
  • nt_memcpy is undefined for overlapping ranges; use nt_memmove for overlap-safe copying.

Build

This project builds a static library:

make

Output:

  • 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      # rebuild

Usage

Example:

#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

Project structure

.
├── 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

Standards / flags

  • C standard: C89
  • Recommended flags: -Wall -Wextra -Werror -pedantic -std=c89

Roadmap (next)

  • Continue to remake the string library functions for learning purposes

License

This project is under BSD 3-Clause License

About

A learning-focused reimplementation of the ANSI C string.h functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published