Skip to content

Focus04/easystring.h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

easystring.h

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.

Installation

To use the library, include the easystring.h header in your C program and link the easystring.c implementation.

#include "easystring.h"

Functions and Examples

1. void *memcpy(void *dest, const void *src, size_t n)

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

2. void *memmove(void *dest, const void *src, size_t n)

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

3. void *memchr(const void *str, int c, size_t n)

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

4. int memcmp(const void *buf1, const void *buf2, size_t n)

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

5. void *memset(void *dest, int c, size_t n)

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

6. char *strcat(char *dest, const char *src)

Concatenates src to the end of dest.

char dest[20] = "Hello, ";
strcat(dest, "world!");
printf("%s\n", dest); // Output: Hello, world!

7. char *strncat(char *dest, const char *src, size_t n)

Appends up to n characters from src to dest.

char dest[20] = "Hello, ";
strncat(dest, "world!!!", 5);
printf("%s\n", dest); // Output: Hello, world

8. char *strchr(const char *src, int c)

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

9. char *strrchr(const char *src, int c)

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

10. int strcmp(const char *ptr1, const char *ptr2)

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

11. int strncmp(const char *ptr1, const char *ptr2, size_t n)

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

12. char *strcpy(char *dest, const char *src)

Copies the string src to dest.

char dest[10];
strcpy(dest, "hello");
printf("%s\n", dest); // Output: hello

13. char *strncpy(char *dest, const char *src, size_t n)

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

14. size_t strlen(const char *s)

Returns the length of s.

char str[] = "hello";
printf("%zu\n", strlen(str)); // Output: 5

15. size_t strspn(const char *str1, const char *str2)

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

16. size_t strcspn(const char *str1, const char *str2)

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

17. char *strpbrk(const char *str1, const char *str2)

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

18. char *strstr(const char *str1, const char *str2)

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

19. char *strtok(char *str, const char *sep)

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!

20. size_t getline(char *str)

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

About

Simple library for operating with strings in c

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages