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: HelloSafely 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: ababcdSearches 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: elloCompares 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: 0Sets 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: AAAAAAAAAAConcatenates 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, worldFinds 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: oFinds 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: loCompares 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: 0Compares 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: 0Copies the string src to dest.
char dest[10];
strcpy(dest, "hello");
printf("%s\n", dest); // Output: helloCopies 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: helloReturns the length of s.
char str[] = "hello";
printf("%zu\n", strlen(str)); // Output: 5Returns the length of the initial segment of str1 that contains only characters in str2.
char str[] = "abcde12345";
printf("%zu\n", strspn(str, "abcde")); // Output: 5Returns 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: 5Finds 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: elloFinds 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: worldTokenizes 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