The easystring.h
library provides a set of functions for common string operations in C. These functions replicate similar functionality to the standard C library string functions, offering manipulation of memory blocks, string concatenation, searching, tokenizing etc.
To use the library, include the easystring.h
header in your C program and link the easystring.c
implementation.
#include "easystring.h"
Copies n
bytes from src
to dest
. Returns a pointer to dest
.
char src[] = "Hello";
char dest[6];
memcpy(dest, src, 6);
printf("%s\n", dest); // Output: Hello
Safely copies n
bytes from src
to dest
, allowing for overlapping regions. Returns a pointer to dest
.
char str[] = "abcdef";
memmove(str + 2, str, 4);
printf("%s\n", str); // Output: ababcd
Searches for the first occurrence of c
in the first n
bytes of str
. Returns a pointer to the byte or NULL
if not found.
char str[] = "hello";
char *ptr = memchr(str, 'e', 5);
printf("%s\n", *ptr); // Output: ello
Compares n
bytes of buf1
and buf2
. Returns 0
if they are equal, a positive value if buf1
> buf2
, or a negative value if buf1
< buf2
.
char a[] = "abc";
char b[] = "abc";
printf("%d\n", memcmp(a, b, 3)); // Output: 0
Sets the first n
bytes of dest
to c
. Returns a pointer to dest
.
char buffer[10];
memset(buffer, 'A', 10);
printf("%.*s\n", 10, buffer); // Output: AAAAAAAAAA
Concatenates src
to the end of dest
.
char dest[20] = "Hello, ";
strcat(dest, "world!");
printf("%s\n", dest); // Output: Hello, world!
Appends up to n
characters from src
to dest
.
char dest[20] = "Hello, ";
strncat(dest, "world!!!", 5);
printf("%s\n", dest); // Output: Hello, world
Finds the first occurrence of c
in src
. Returns a pointer to c
or NULL
.
char str[] = "hello";
char *ptr = strchr(str, 'o');
printf("%s\n", ptr); // Output: o
Finds the last occurrence of c
in src
. Returns a pointer to c
or NULL
.
char str[] = "hello";
char *ptr = strrchr(str, 'l');
printf("%s\n", ptr); // Output: lo
Compares the strings ptr1
and ptr2
. Returns 0
if equal, a positive value if ptr1
> ptr2
, or a negative value if ptr1
< ptr2
.
char a[] = "hello";
char b[] = "hello";
printf("%d\n", strcmp(a, b)); // Output: 0
Compares up to n
characters of ptr1
and ptr2
. Returns 0
if equal, a positive value if ptr1
> ptr2
, or a negative value if ptr1
< ptr2
.
char a[] = "hello";
char b[] = "help";
printf("%d\n", strncmp(a, b, 3)); // Output: 0
Copies the string src
to dest
.
char dest[10];
strcpy(dest, "hello");
printf("%s\n", dest); // Output: hello
Copies up to n
characters from src
to dest
. Pads with null bytes if src
is shorter than n
.
char dest[10];
strncpy(dest, "hello", 10);
printf("%s\n", dest); // Output: hello
Returns the length of s
.
char str[] = "hello";
printf("%zu\n", strlen(str)); // Output: 5
Returns the length of the initial segment of str1
that contains only characters in str2
.
char str[] = "abcde12345";
printf("%zu\n", strspn(str, "abcde")); // Output: 5
Returns the length of the initial segment of str1
that does not contain any characters from str2
.
char str[] = "hello, world!";
printf("%zu\n", strcspn(str, ", ")); // Output: 5
Finds the first occurrence in str1
of any character in str2
. Returns a pointer to the character in str1
, or NULL
if none found.
char str[] = "hello";
char *ptr = strpbrk(str, "aeiou");
printf("s\n", *ptr); // Output: ello
Finds the first occurrence of str2
in str1
. Returns a pointer to the beginning of str2
in str1
, or NULL
if not found.
char str[] = "hello world";
char *ptr = strstr(str, "world");
printf("%s\n", ptr); // Output: world
Tokenizes str
using sep
as the delimiter. Successive calls with NULL
continue tokenization.
char str[] = "Hello, world!";
char *token = strtok(str, ", ");
while (token) {
printf("%s\n", token);
token = strtok(NULL, ", ");
}
// Output:
// Hello
// world!
Dynamically allocates memory for a string of unknown length before the \n
(newline) character.
char *input = malloc(0)
int length = getline(input);
printf("You entered: %s\n", input);
printf("String length: %d\n", length);
free(input);
// Input:
// Hello, world!
// Output:
// You entered: Hello, world!
// String length: 13